Demonstrating the difference between push_back() and push_front() : list push pop « List « C++






Demonstrating the difference between push_back() and push_front()

 
 
#include <iostream>
#include <list>
using namespace std;

int main()
{
  list<int> lst1, lst2;
  int i;

  for(i=0; i<10; i++) lst1.push_back(i);
  for(i=0; i<10; i++) lst2.push_front(i);
  
  list<int>::iterator p;
  
  cout << "Contents of lst1:\n";
  p = lst1.begin();  
  while(p != lst1.end()) {
    cout << *p << " ";
    p++;
  }
  cout << "\n\n";

  cout << "Contents of lst2:\n";
  p = lst2.begin();  
  while(p != lst2.end()) {
    cout << *p << " ";
    p++;
  }
    
  return 0;
}

 /* 
Contents of lst1:
0 1 2 3 4 5 6 7 8 9

Contents of lst2:
9 8 7 6 5 4 3 2 1 0
 */       
  








Related examples in the same category

1.list: push_back and push_front
2.list: push_back, front, empty and pop_front
3.list.pop_front(): remove element from front
4.list.pop_back(): remove element from back