#include<iostream> #include<vector> classA { public: A (int x_arg) : x (x_arg) { std::cout << "A (x_arg)\n"; } A () { x = 0; std::cout << "A ()\n"; } A (const A &rhs) noexcept { x = rhs.x; std::cout << "A (A &)\n"; } A (A &&rhs) noexcept { x = rhs.x; std::cout << "A (A &&)\n"; } ~A() { std::cout << "~A ()\n"; }
private: int x; };
voidtest_emplace_back_1() { // For emplace_back constructor A (int x_arg) will be called. // And for push_back A (int x_arg) is called first and // move A (A &&rhs) is called afterwards { std::vector<A> a; std::cout << "call emplace_back:\n"; a.emplace_back(0); // (1) direct object creation inside vector }
{ std::vector<A> a; std::cout << "call push_back:\n"; a.push_back(1); // (1) create temp object and // (2) then move copy to vector and // (3) free temp object } } /* call emplace_back: A (x_arg) ~A () call push_back: A (x_arg) A (A &&) ~A () ~A () */
voidtest_emplace_back_2() { // emplace_back and push_back for `A(0)`, it's same. // A (int x_arg) is called first and // move A (A &&rhs) is called afterwards { std::vector<A> a; std::cout << "call emplace_back:\n"; a.emplace_back(A(0)); // (1) create temp object and // (2) then move copy to vector and // (3) free temp object }
{ std::vector<A> a; std::cout << "call push_back:\n"; a.push_back(A(1)); // (1) create temp object and // (2) then move copy to vector and // (3) free temp object } }
/* call emplace_back: A (x_arg) A (A &&) ~A () ~A () call push_back: A (x_arg) A (A &&) ~A () ~A () */
voidtest_emplace_back_3() { // emplace_back and push_back for `A obj(0)`, it's same. // A (int x_arg) is called first and // copy constructor A (A &) is called afterwards { std::vector<A> a; std::cout << "call emplace_back:\n"; A obj(0); a.emplace_back(obj); // copy constructor to vector }
{ std::vector<A> a; std::cout << "call push_back:\n"; A obj(1); a.push_back(obj); // copy constructor to vector } } /* call emplace_back: A (x_arg) A (A &) ~A () ~A () call push_back: A (x_arg) A (A &) ~A () ~A () */
extract subvector
1 2 3
vector<int>::const_iterator first = myVec.begin() + 10; vector<int>::const_iterator last = myVec.begin() + 15; vector<int> newVec(first, last); // [10,15)