#include#include using namespace std; int main() { vector v = {1, 2, 3}; // create a vector of 3 integers v.push_back(4); // add an integer (4) to the end of the vector for(int i=0; i
This code snippet creates a vector of 3 integers and then adds an additional integer (4) to the end using the push_back() function. It then iterates through the vector and prints each integer separated by a space.
Example 2: Creating a vector of strings#includeThis code snippet creates an empty vector of strings and then adds three strings to the end using the push_back() function. It then iterates through the vector using a range-based for loop and prints each string separated by a space. Package Library: STL (Standard Template Library)#include #include using namespace std; int main() { vector v; // create an empty vector of strings v.push_back("apple"); // add a string to the end of the vector v.push_back("banana"); v.push_back("cherry"); for(string s : v) // iterate through the vector using the range-based for loop cout << s << " "; // print each string separated by a space return 0; } // Output: apple banana cherry