#include#include int main() { std::vector myVector; myVector.push_back(10); myVector.push_back(20); myVector.push_back(30); for (int i = 0; i < myVector.size(); i++) { std::cout << myVector[i] << " "; } return 0; }
#includeHere, we create a custom class called Person and use it to create two objects. We then create a vector of Person objects and use push_back to add the two Person objects we created to it. We then loop through the vector and print the name and age of each person. This code also uses the STL package from the C++ standard library. In summary, the push_back method is a helpful tool for adding elements to the end of a vector object in C++. It is included in the STL package, which is part of the C++ standard library.#include class Person { public: std::string name; int age; }; int main() { std::vector people; Person person1; person1.name = "John"; person1.age = 25; Person person2; person2.name = "Jane"; person2.age = 30; people.push_back(person1); people.push_back(person2); for (int i = 0; i < people.size(); i++) { std::cout << people[i].name << " is " << people[i].age << " years old." << std::endl; } return 0; }