C++ List Library - push_back() Function



Description

The C++ function std::list::push_back() inserts new element at the end of list and increases size of list by one.

Declaration

Following is the declaration for std::list::push_back() function form std::list header.

C++11

void push_back (value_type&& val);

Parameters

val − Value of element to be inserted into list.

Return value

None.

Exceptions

This member function never throws exception.

Time complexity

Constant i.e. O(1)

Example

The following example shows the usage of std::list::push_back() function.

#include <iostream>
#include <list>

using namespace std;

int main(void) {
   list<int> l1 = {1, 2, 3, 4, 5};
   list<int> l2;

   for (auto it = l1.begin(); it != l1.end(); ++it)
      l2.push_back(move(*it));

   cout << "List l2 contains following elements" << endl;

   for (auto it = l2.begin(); it != l2.end(); ++it)
      cout << *it << endl;

   return 0;
}

Let us compile and run the above program, this will produce the following result −

List l2 contains following elements
1
2
3
4
5
list.htm
Advertisements