FranXh
FranXh

Reputation: 4771

C++ vector of vectors declaration

I am not really sure what this line of code does.

 vector<vector<A>> someth;

Does this mean that creates a vector with elements vectors with objects of class A? Can please someone explain this?

Thanks

Upvotes: 3

Views: 4728

Answers (4)

myin528
myin528

Reputation: 522

Correct. Basically

someth[i]

returns a vector. And

someth[i][j]

returns an A.

One thing needs to pay attention to is that there must be a space between two '>'s. Should be

vector<vector<A> > someth;

Upvotes: 3

Not_a_Golfer
Not_a_Golfer

Reputation: 49177

yes, that's exactly what it means, it's a vector of vectors of A's.

Upvotes: 1

greg
greg

Reputation: 4953

Yes, this creates a vector whose elements are vectors whose elements are A objects. It's worth noting that this declaration is only valid in C++11. Before then, the >> was interpreted as the symbol >> (read from). It needs to be declared vector<vector<A> > someth; (with a space).

Upvotes: 4

Luchian Grigore
Luchian Grigore

Reputation: 258548

Yes.

Also note that your syntax is not supported before C++11:

vector<vector<A> > someth;

is the pre-C++11 syntax. Before that, >> was treated as the bit-shift operator, so you'd get a compiler error on some compilers.

This actually creates an empty vector that can contain objects of type vector<A>.

Upvotes: 8

Related Questions