Given that the vector is a class member, you're better off using an initialiser list
YourClass::YourClass(std::size_t vertices) : adjacencyList(vertices, std::vector<int>())
{
}
since it invokes the constructor for the vector<vector<int> >
directly.
Doing assignment
YourClass::YourClass(std::size_t vertices)
{
adjacencyList = vector<vector<int> >(vertices, vector<int>());
}
is actually functionally equivalent to
YourClass::YourClass(std::size_t vertices) : adjacencyList()
{
adjacencyList = vector<vector<int> >(vertices, vector<int>());
}
since all members and bases are implicitly constructed before entering the body of YourClass::YourClass
. (The standard has rules for order of construction of multiple bases and members, but the result is still the same).
This means your approach constructs adjacencyList
, creates a temporary vector<vector<int> >
, and invokes the assignment operator to effectively reinitialise adjacencyList
. The best case (assuming vector
s constructors and operators are sanely implemented, which most are) is that this approach has no more overhead than initialising directly. The worst case, since it is constructing more temporary objects and the compiler might not optimise them out of existence, is greater overhead.