how to add an element to the list in c++ code example

Example 1: c++ append to list

// list::push_back
#include 
#include 

int main ()
{
  std::list mylist;
  int myint;

  std::cout << "Please enter some integers (enter 0 to end):\n";

  do {
    std::cin >> myint;
    mylist.push_back (myint);
  } while (myint);

  std::cout << "mylist stores " << mylist.size() << " numbers.\n";

  return 0;
}

Example 2: c++ list add

list myList = list();

//add a 4 to the end of the list
myList.push_back(4);

//add a 5 to the begining of the list
myList.push_front(5);

Tags:

Misc Example