std::vector::reserve performance penalty

后端 未结 6 712
感动是毒
感动是毒 2021-01-05 02:19
inline void add(const DataStruct& rhs) {
   using namespace boost::assign;
   vec.reserve(vec.size() + 3);
   vec += rhs.a, rhs.b, rhs.c;
}

The

6条回答
  •  隐瞒了意图╮
    2021-01-05 02:49

    Use only reserve if you know in advance how much place it will use.

    Reserve will need to copy your whole vector...

    If you do a push_back and the vector is too small, then it will do a reserve (vec.size()*2).

    If you don't know beforehand how big your vector is going to be and if you need random access, consider using std::deque.

提交回复
热议问题