vector

  • Use reserve when you know the vector size, it can prevent to re-allocate memory and also redundant copy

    A a, b, c;
    vector<A> v;
    v.push_back(std::move(a));
    v.push_back(std::move(b));
    v.push_back(std::move(c));
    

    It has 3 move cstor and 1+2 copy cstor

    A a, b, c;
    vector<A> v;
    v.reserve(3);
    v.push_back(std::move(a));
    v.push_back(std::move(b));
    v.push_back(std::move(c));
    

    It has 3 move cstor

  • Repalce push_back with emplace_back

    class A {
      public:
          A(int a) : _a(a) { }
      private:
          int _a;
    }; 
    vector<A> v;
    v.reserve(2)
    v.push_back(A(0)); //one cstor and one copy/move cstor
    v.emaplace_back(0); //one cstor
    

results matching ""

    No results matching ""