Solved

Array of vectors containing integers

Posted on 2011-03-08
5
293 Views
Last Modified: 2012-05-11
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
Comment
Question by:panJames
  • 2
  • 2
5 Comments
 
LVL 53

Expert Comment

by:Infinity08
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));

Open in new window

0
 

Author Comment

by:panJames
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

by:
Infinity08 earned 500 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 33

Expert Comment

by:sarabande
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 33

Expert Comment

by:sarabande
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
why "." vs "->" 23 120
mixing C++ & C# in Vis Studio 2013 7 172
Unable to start eclipse ? 17 141
Least Squares Curve Fitting 4 77
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question