#include#include int main() { std::vector v; v.emplace_back(1); v.emplace_back(2); v.emplace_back(3); for (int i : v) { std::cout << i << ' '; } std::cout << '\n'; return 0; }
1 2 3
#include#include struct Person { std::string name; int age; bool is_student; }; int main() { std::vector people; people.emplace_back("Alice", 25, true); people.emplace_back("Bob", 30, false); for (const auto& p : people) { std::cout << p.name << " is " << p.age << " years old"; if (p.is_student) { std::cout << " and is a student.\n"; } else { std::cout << " and is not a student.\n"; } } return 0; }
Alice is 25 years old and is a student. Bob is 30 years old and is not a student.This example shows how to use emplace_back() to add objects to a vector without manually allocating memory for it. Package library: Standard C++ library.