C++ List push_back
Adding a new element at the end of a C++ list is achieved by using the push_back method, which results in an increment of the list container's size by one.
The push_back method adds the element 5 to the back of the container.
Syntax
Suppose a element is 'x':
push_back(const value_type& x);
Parameter
x : It is the value to be inserted at the end.
Return value
It does not return any value.
Example 1
Let's see a simple example
#include <iostream>
#include<list>
using namespace std;
int main()
{
list<string> li={"C"," C++"," .Net"};
list<string>::iterator itr;
li.push_back(" java");
li.push_back(" PHP");
for(itr=li.begin();itr!=li.end();++itr)
cout<<*itr;
return 0;
}
Output:
C C++ .Net java PHP
In this instance, the push_back method appends two strings, namely Java and PHP, to the end of the list.
Example 2
Let's see a simple example
#include <iostream>
#include<list>
using namespace std;
int main()
{
list<int> li={6,7,8,9};
list<int>::iterator itr;
li.push_back(10);
li.push_back(11);
for(itr=li.begin();itr!=li.end();++itr)
cout<<*itr<<","
return 0;
}
Output:
6,7,8,9,10,11