#include#include int main() { std::list
myList; // create an empty list myList.push_back(1); // add elements to the back myList.push_back(2); myList.push_back(3); myList.push_front(0); // add elements to the front myList.push_front(-1); return 0; }
#include#include int main() { std::list
myList = { 1, 2, 3, 4, 5 }; myList.pop_front(); // remove element from the front myList.pop_back(); // remove element from the back myList.remove(3); // remove all elements equal to 3 return 0; }
#includeIn all of the above examples, we see the use of the STL's List container class, which is defined in the#include int main() { std::list
myList = { 1, 2, 3, 4, 5 }; // iterate over each element using a for-each loop for (auto& elem : myList) std::cout << elem << ' '; std::cout << '\n'; // iterate over each element using an iterator for (auto it = myList.begin(); it != myList.end(); ++it) std::cout << *it << ' '; std::cout << '\n'; return 0; }