cpp std list example

Solutions on MaxInterview for cpp std list example by the best coders in the world

showing results for - "cpp std list example"
Alejandra
12 Jul 2018
1#include <algorithm>
2#include <iostream>
3#include <list>
4 
5int main()
6{
7    // Create a list containing integers
8    std::list<int> l = { 7, 5, 16, 8 };
9 
10    // Add an integer to the front of the list
11    l.push_front(25);
12    // Add an integer to the back of the list
13    l.push_back(13);
14 
15    // Insert an integer before 16 by searching
16    auto it = std::find(l.begin(), l.end(), 16);
17    if (it != l.end()) {
18        l.insert(it, 42);
19    }
20 
21    // Print out the list
22    std::cout << "l = { ";
23    for (int n : l) {
24        std::cout << n << ", ";
25    }
26    std::cout << "};\n";
27}