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

C++ push_back function

C++ push_back function

C++ Container Class The C++ container class


Among many containers,list (doubly linked list) is a data structure with efficient insertion and deletion, and push_back is the key function for adding elements to the end of the linked list.

push_back is a member function, used tolinked list (list) 'sendadd a new element.

push_back implements the linked list'sdynamic growth; you do not need to specify the container's final size in advance, and can add elements anytime as needed at runtime.

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


Basic syntax and parameters

push_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 push_back(const T& value);

void push_back(T&& value);  // C++11 之后(支持右值)

Parameter Description

- Type: Consistent with the element type specified when the container was defined, or a value that can be converted to that type. - Description: The value you want to add to the end of the container. It can be a variable, a literal (such as `10`, `"hello"`), or the result of an expression.

Function description


Example

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

Example 1: Basic usage - managing integers

Example

#include

#include // 1. Must include the list header file

int

main

(

)

{

   

// 2. Create an empty list to store integers

    std

::

list

<

int

numbers

;

    std

::

cout

<<

"Initially, the size of the list is: "

<<

numbers.

size

(

)

<<

std

::

endl

;

   

// 3. Use push_back to add elements

    numbers.

push_back

(

10

)

;

// Add integer 10 at the end

    numbers.

push_back

(

20

)

;

// Add 20 after 10

    numbers.

push_back

(

30

)

;

// Add 30 after 20

    std

::

cout

<<

"After adding, the size of the list is: "

<<

numbers.

size

(

)

<<

std

::

endl

;

   

// 4. Traverse and output all elements

    std

::

cout

<<

"The elements in the list are: "

;

   

for

(

int

n

:

numbers

)

{

        std

::

cout

<<

n

<<

" "

;

   

}

    std

::

cout

<<

std

::

endl

;

   

return

0

;

}

Expected output:

初始时,list的大小是: 0

添加后,list的大小是: 3

list中的元素是: 10 20 30

Code analysis:

  1. #include is the prerequisite for using list the container.
  2. std::list numbers; creates a numbers empty list, which will store in the future int integers of type.
  3. three times push_back calls, sequentially adding 10, 20, 30 to numbers 's end. The addition order is the final order of elements in list .
  4. numbers.size() returns the current element count.

Example 2: Managing strings

list can store not only numbers but also strings, custom types, and any other data type.

Example

#include

#include

#include

int

main

(

)

{

   

// Create a list that stores std::string

    std

::

list

<

std

::

string

tasks

;

   

// Use push_back to add to-do items

    tasks.

push_back

(

"Learn C++ list"

)

;

    tasks.

push_back

(

"Do homework"

)

;

    tasks.

push_back

(

"Read technical documentation"

)

;

    std

::

cout

<<

"Today's to-do items ("

<<

tasks.

size

(

)

<<

" items):"

<<

std

::

endl

;

   

for

(

const

auto

&

task

:

tasks

)

{

        std

::

cout

<<

"- "

<<

task

<<

std

::

endl

;

   

}

   

return

0

;

}

Expected output:

今日待办事项 (3 项):

- 学习 C++ list

- 写作业

- 阅读技术文档

Code analysis:

Example 3: Using together with push_front

push_back and push_front can be used together to build a linked list.

Example

#include

#include

int

main

(

)

{

    std

::

list

<

int

data

;

   

// First add at the head

    data.

push_front

(

30

)

;

    data.

push_front

(

20

)

;

    data.

push_front

(

10

)

;

   

// Then add at the tail

    data.

push_back

(

40

)

;

    data.

push_back

(

50

)

;

    std

::

cout

<<

"Elements: "

;

   

for

(

int

n

:

data

)

{

        std

::

cout

<<

n

<<

" "

;

   

}

    std

::

cout

<<

std

::

endl

;

   

return

0

;

}

Expected output:

元素: 10 20 30 40 50

Code analysis:


C++ Container Class The C++ container class