#include#include int main() { std::vector v{1, 2, 3}; v.push_back(4); v.push_back(5); for(auto num : v) { std::cout << num << " "; } return 0; } // Output: 1 2 3 4 5
#includeIn this example, we first initialize a list l with the elements {1, 2, 3}. We then use the push_back() function twice to add the integers 4 and 5 to the back of the list. Finally, we use a range-based for loop to iterate over the list and print its elements. Package Library: STL (Standard Template Library) Both examples use the standard template library of C++.#include int main() { std::list
l{1, 2, 3}; l.push_back(4); l.push_back(5); for(auto num : l) { std::cout << num << " "; } return 0; } // Output: 1 2 3 4 5