Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C++ pop_back function

C++ pop_back function

C++ Container Class The C++ container class


Among many container operations,pop_back is a very practical function, used toremoves the last element of the vector.

pop_back is a member function of the container class, used to delete the last element of the container; the container's size() decreases by 1.

pop_backimplements the container'sdynamic shrinking, allowing you to remove unneeded trailing elements at any time.

Word meaning: pop is pop,back is back; together they mean to pop the trailing element, which is very intuitive.


Basic syntax and parameters

pop_back is a member function of the container class, so you need to have a container object first, then use the dot operator . to call it.

Syntax format



void pop_back();

Parameter Description

- `pop_back` takes no arguments.

Function description


Example

Let us thoroughly master pop_back through a series of examples from simple to complex.

Example 1: Basic usage - remove the last element

Example

#include

#include // 1. Must include the vector header

int

main

(

)

{

   

// 2. Create a vector and add some elements

    std

::

vector

<

int

numbers

=

{

10

,

20

,

30

,

40

,

50

}

;

    std

::

cout

<<

"Initially, the size of the vector is: "

<<

numbers.

size

(

)

<<

std

::

endl

;

    std

::

cout

<<

"Elements are: "

;

   

for

(

int

n

:

numbers

)

{

        std

::

cout

<<

n

<<

" "

;

   

}

    std

::

cout

<<

std

::

endl

;

   

// 3. Use pop_back to delete the last element

    numbers.

pop_back

(

)

;

// Delete the last element (50)

    std

::

cout

<<

"After deleting one element, the size is: "

<<

numbers.

size

(

)

<<

std

::

endl

;

    numbers.

pop_back

(

)

;

// Delete the last element (40)

    std

::

cout

<<

"After deleting another element, the size is: "

<<

numbers.

size

(

)

<<

std

::

endl

;

   

// 4. Traverse and output all elements

    std

::

cout

<<

"Remaining elements are: "

;

   

for

(

int

n

:

numbers

)

{

        std

::

cout

<<

n

<<

" "

;

   

}

    std

::

cout

<<

std

::

endl

;

   

return

0

;

}

Expected output:

初始时,vector的大小是: 5

元素是: 10 20 30 40 50

删除一个元素后,大小是: 4

再删除一个元素后,大小是: 3

剩余的元素是: 10 20 30

Code analysis:

  1. #include is the prerequisite for using vector the container.
  2. std::vector numbers = {10, 20, 30, 40, 50}; created a containing 5 elements vector.
  3. twice pop_back calls, sequentially deleting the trailing 50 and 40.
  4. after each call, the container's size() decreases by 1.

Example 2: Safe use of pop_back - check whether the container is empty

When calling pop_back Before using, it is best to check whether the container is empty to avoid undefined behavior.

Example

#include

#include

#include

int

main

(

)

{

   

// Create a vector storing strings

    std

::

vector

<

std

::

string

tasks

=

{

"Learn C++"

,

"Do homework"

,

"Read documentation"

}

;

    std

::

cout

<<

"Number of to-do items: "

<<

tasks.

size

(

)

<<

std

::

endl

;

   

// Safely delete the last element

   

while

(

!

tasks.

empty

(

)

)

{

        std

::

cout

<<

"Completed: "

<<

tasks.

back

(

)

<<

std

::

endl

;

        tasks.

pop_back

(

)

;

// Delete the last element

        std

::

cout

<<

"Remaining to-do items: "

<<

tasks.

size

(

)

<<

std

::

endl

;

   

}

   

// Try to delete again (container is now empty)

   

if

(

tasks.

empty

(

)

)

{

        std

::

cout

<<

"No more to-do items!"

<<

std

::

endl

;

   

}

   

return

0

;

}

Expected output:

待办事项数量: 3

完成: 阅读文档

剩余待办事项: 2

完成: 写作业

剩余待办事项: 1

完成: 学习 C++

剩余待办事项: 0

没有更多待办事项了!

Code analysis:

Example 3: Use pop_back to implement a simple stack

push_back and pop_back used together, can implement a simple stack structure.

Example

#include

#include

class

Stack

{

private

:

    std

::

vector

<

int

data

;

public

:

   

// Push

   

void

push

(

int

value

)

{

        data.

push_back

(

value

)

;

   

}

   

// Pop

   

void

pop

(

)

{

       

if

(

!

data.

empty

(

)

)

{

            data.

pop_back

(

)

;

       

}

   

}

   

// Get the top element of the stack

   

int

top

(

)

{

       

if

(

!

data.

empty

(

)

)

{

           

return

data.

back

(

)

;

       

}

       

return

1

;

// Indicates the stack is empty

   

}

   

// Check whether the stack is empty

   

bool

empty

(

)

{

       

return

data.

empty

(

)

;

   

}

   

// Get the size of the stack

   

size_t

size

(

)

{

       

return

data.

size

(

)

;

   

}

}

;

int

main

(

)

{

    Stack s

;

   

// Push operation

    s.

push

(

10

)

;

    s.

push

(

20

)

;

    s.

push

(

30

)

;

    std

::

cout

<<

"Stack size: "

<<

s.

size

(

)

<<

std

::

endl

;

    std

::

cout

<<

"Top element: "

<<

s.

top

(

)

<<

std

::

endl

;

   

// Pop operation

    s.

pop

(

)

;

    std

::

cout

<<

"After popping, top element: "

<<

s.

top

(

)

<<

std

::

endl

;

    s.

pop

(

)

;

    std

::

cout

<<

"After another pop, top element: "

<<

s.

top

(

)

<<

std

::

endl

;

    s.

pop

(

)

;

    std

::

cout

<<

"After another pop, is the stack empty: "

<<

(

s.

empty

(

)

?

"Yes"

:

"No"

)

<<

std

::

endl

;

   

return

0

;

}

This example shows how to use pop_back to implement the stack's pop operation.push_back corresponds to push,pop_back corresponds to pop.


C++ Container Class The C++ container class