[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
Solved

# Array of vectors containing integers

Posted on 2011-03-08
Medium Priority
298 Views
Hello experts,

I would like to have 2D array containing lists of integers.

I tried this one:

std::vector<int> linesGrid [64, 48];

but it does not compile.

What do I do wrong?

Thank you

panJames
0
Question by:panJames
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2
• 2

LVL 53

Expert Comment

ID: 35069913
If you want to do that with vectors, you'll need a definition that looks somewhat like this :
``````std::vector<std::vector<int> > linesGrid(64, std::vector<int>(48));
``````
0

Author Comment

ID: 35070083
WOW!

I am trying to get my head around it.

1. std::vector<std::vector<int>> linesGrid;

It my understanding: linesGrid is the list of (list of integers)

2. linesGrid(64, std::vector<int>(48));

In my understanding it looks like a function call with two variables where the second one is again a function???

I am so confused here... :(

panJames
0

LVL 53

Accepted Solution

Infinity08 earned 2000 total points
ID: 35070300
>> It my understanding: linesGrid is the list of (list of integers)

More accurately : a vector of vectors of ints.

>> In my understanding it looks like a function call with two variables where the second one is again a function???

It's calling the constructor of std::vector, where the first argument is the amount of items you want in the vector, and the second argument is how those items should be initialized.

Refer to :

http://www.cplusplus.com/reference/stl/vector/vector/
0

LVL 35

Expert Comment

ID: 35070301
the point is that std::vector is a 1d-array. to get a 2d-array you do a vector of row vectors.

Sara

0

LVL 35

Expert Comment

ID: 35070389
actually it is not really a grid like int grid[64][48]  cause each row vector individually could grow or shrink. but if you do an initialisation like infinity08 showed and don't do a push_back or erase on any of the row vectors you pretty well have the same behavior than with a fixed-sized int 2d-array.

Sara
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
###### Suggested Courses
Course of the Month13 days, 8 hours left to enroll