Search code examples
c++stlstdvector

Populate vector with protobuf objects in the constructor


Is there a 1-line way to do that directly via the vector constructor?

std::vector<someproto> vector_protos;
vector_protos.emplace_back();

Something like vector_protos(1).


Solution

  • std::vector has a constructor, that accepts count - i.e. number of elements (see item (4) in the link):

    (4) Constructs the container with count default-inserted instances of T. No copies are made.

    To construct vector_protos with 1 default contructed element you can use:

    std::vector<someproto> vector_protos(1);