#include#include int main() { std::vector vec {1, 2, 3, 4}; vec.push_back(5); for (int i = 0; i < vec.size(); i++) { std::cout << vec[i] << " "; } return 0; }
1 2 3 4 5
#include#include class Person { public: std::string name; int age; Person(std::string name, int age) { this->name = name; this->age = age; } }; int main() { std::vector people; people.push_back(Person("John", 30)); people.push_back(Person("Mary", 25)); for (int i = 0; i < people.size(); i++) { std::cout << people[i].name << ", " << people[i].age << std::endl; } return 0; }
John, 30 Mary, 25In the above code, we create a `Person` class with two member variables: `name` and `age`. We then create a vector `people` to hold instances of the `Person` class. We add two instances of the `Person` class to the vector using two calls to `push_back()`. Finally, we print out the contents of the vector to verify that the objects were added correctly. Package library: The `std` library of C++.