#include#include using namespace std; int main() { vector vec; // Creates an empty vector // Add elements using push_back() vec.push_back(10); vec.push_back(20); vec.push_back(30); // Accessing elements cout << "Elements in vector: "; for(int i=0; i
In this example, we use the STL (Standard Template Library) vector class to create an empty vector of integers, then use the push_back() method to add three elements to the vector: 10, 20, and 30. Finally, we access the elements of the vector using a for loop and display them.
Example 2:#include#include using namespace std; int main() { array arr = {1, 2, 3, 4, 5}; // Add an element using push_back() arr.push_back(6); // Accessing elements cout << "Elements in the array: "; for(int i=0; i In this example, we use the C++11 std::array container to create an array of integers with a fixed size of 5, then try to add an element using the push_back() method. However, this produces an error at compilation because an array with a fixed size cannot be resized by adding elements to it. The package library for the STL vector class is , whereas for the std::array container is . These packages are a part of the Standard Template Library (STL) which consists of a collection of C++ template classes to provide common data structures and algorithms.