list c++ example
Example 1: cpp std list example
#include <algorithm>
#include <iostream>
#include <list>
int main()
{
std::list<int> l = { 7, 5, 16, 8 };
l.push_front(25);
l.push_back(13);
auto it = std::find(l.begin(), l.end(), 16);
if (it != l.end()) {
l.insert(it, 42);
}
std::cout << "l = { ";
for (int n : l) {
std::cout << n << ", ";
}
std::cout << "};\n";
}
Example 2: how to write C++ list
#include <iostream>
#include <list>
int main ()
{
std::list<int> first;
std::list<int> second (4,100);
std::list<int> third (second.begin(),second.end());
std::list<int> fourth (third);
int myints[] = {16,2,77,29};
std::list<int> fifth (myints, myints + sizeof(myints) / sizeof(int) );
std::cout << "The contents of fifth are: ";
for (std::list<int>::iterator it = fifth.begin(); it != fifth.end(); it++)
std::cout << *it << ' ';
std::cout << '\n';
return 0;
}