Johan Råde
Johan Råde

Reputation: 21418

Initializing vector of vectors

Is the following code

vector<vector<int> > v(3,5);

legal C++ 98? Is it legal C++11?

It compiles with MSVS 2010 and gives a vector of size 3, each of which elements is a vector of size 5. It fails to compile with MSVS 2013 and MSVS "14".

Upvotes: 0

Views: 215

Answers (1)

Kerrek SB
Kerrek SB

Reputation: 477660

The one-argument size constructor of std::vector is explicit, so just 5 won't work, as it is not implicitly convertible to std::vector<int>. You need this:

std::vector< std::vector<int> > v(3, std::vector<int>(5));

This has always been the case since C++98 and hasn't changed since (although the actual constructor signatures have changed regarding default arguments; see the cppreference entry for a history of the signatures).

Upvotes: 4

Related Questions