简体   繁体   中英

What does a vector of vectors look like?

I'm just getting started with vectors. From what I know, to declare a vector of characters called sentences with a size of 60 it would look like " vector < char >Ages(60); "

But what if I was trying to make a vector of sentences (same as above) called pages with an initial size of 50.

Would it look like " vector< char >pages(50, vector< char >sentences(60)) "?

And then a vector of a vector of a vector, for example, a vector of pages called book with an initial size of 0.

Attempt: vector < char > book(0, vector< char> book(50, vector< char> sentences(60)))

I'm confused about the syntax.

vector<Type> creates a vector of Type objects. So, a vector of a vector of Type , would be vector<vector<Type>> . The constructor you are using for vector<Type> takes a size_t and a default Type . If you want to make a vector of a vector with size 50 , while the innermost vector s have size 60 , you would do

vector<vector<Type>>(50, vector<Type>(60));
                     ^^     ^^^^^^^^^^^^
                 (size_t)  (default vector<Type> value)

The other posters are correct--simply nest whatever you want inside the <> brackets. You can go deeper:

vector< vector< vector< char > > > Book;

Furthermore, it's worth noting that one of the primary advantages of vectors over arrays is that there is no need to specify a size up front. Unless you have a very specific case, just let C++ manage the size for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM