Multi-Dimensional Vectors

Pages: 1234
Jul 19, 2010 at 3:58am
Hey. I am trying to create a program that holds coordinates for a map. I've tried multi-dimensional arrays, but there are too many coordinates for the array to hold (9 million coordinates). I was referred to multi-dimensional vectors.

I have looked up tutorials and books and everything, but I just don't get it. Can someone please explain to me how to use vectors and explain what the heck its doing. :) Please? Thank you in advance.

Email: tyler.is.number.one@gmail.com

-Tyler
Jul 19, 2010 at 7:14am
A 2D vector is a vector of vectors eg: vector < vector < int > >
But I think that you'd need a single vector of pairs eg: vector < pair < int, int > > ( or of your own struct/class holding the coordinates values instead of std::pair ).
Jul 19, 2010 at 7:27pm
Does this single vector of pairs work the same way as a string array? string array[int][int]
Jul 19, 2010 at 7:59pm
If you want an array like this -> T array[x][y] (where T is a data type and x,y are ints)

You can do it using vectors like this:

1
2
3
4
5
6
7
8
vector< vector < T > > array;

array.resize(x);
for (int i=0; i<array.size(); i++)
    array[i].resize(y);

//you can now use the syntax
//array[i][j] to access the elements 

More info on vectors -> http://cplusplus.com/reference/stl/vector/
Jul 19, 2010 at 8:01pm
Do you want your array to be your 'coordinate space' (one array element for every coordinate), or do you just want to store a list of coordinates that map onto a different space?

Eg:

1
2
3

string place[100][100]; // 100000 individual places addressable by coordinate


Or:

1
2
3
4
5
6
struct coord
{
    int x, y;
};

coord locations[100]; // store 100 locations 
Jul 19, 2010 at 8:25pm
@m4ster r0shi: where does 'j' come from?

@galik: I want it to be like the first choice. Multi-dimensional. Two numbers for one spot.
Jul 19, 2010 at 8:32pm
Wander wrote:
@m4ster r0shi: where does 'j' come from?

Just put indices (indexes) of your choice there. I just wanted to show that after proper initialization you can use it exactly as you would use a multi-dimensional array.
Jul 19, 2010 at 8:33pm
@m4ster r0shi: oh okay, well what exactly is that snippet doing?
Jul 19, 2010 at 8:42pm
It reserves memory for the individual elements. vector < vector < T > > actually means a vector of vectors of T.

The outer vector is used to hold the rows. Each inner vector is a row.

This -> array.resize(x); makes your array have x rows.

This loop here:

1
2
for (int i=0; i<array.size(); i++)
    array[i].resize(y);

makes every row have y elements.

So, now that you have x rows and y elements for every row, you have a total of x*y elements, which is exactly what you want.
Jul 19, 2010 at 8:44pm
oh okay, so then i just type: array[x][y] to access a certain coordinate?
Jul 19, 2010 at 8:46pm
Yes. After you do the initialization, yes.
Jul 19, 2010 at 8:46pm
okay thanks. lemme go try it out.
Jul 19, 2010 at 8:51pm
@Wander
Yes!

@m4ster r0shi
You can use this:
std::vector<std::vector<int> > array(x, std::vector<int>(y));

Which initialises x by y elements.
Last edited on Jul 19, 2010 at 8:52pm
Jul 19, 2010 at 8:55pm
@m4ster roshi: I'm getting tons of weird errors with this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main()
{
    int x, y;

    vector< vector<string> > mapCoords[x][y];

    cout << mapCoords.size() << endl;

    mapCoords.resize(x);

    for (int i=0; i<mapCoords.size(); i++)
        mapCoords[i].resize(y);

    cout << mapCoords.size() << endl;

    return 0;
}

It doesnt seem to like my member functions.

@galik: can you try to explain to me, your method so that way I can use yours and r0shis in the future?
Last edited on Jul 19, 2010 at 8:57pm
Jul 19, 2010 at 8:58pm
Galik wrote:
You can use this:
std::vector<std::vector<int> > array(x, std::vector<int>(y));

But... doesn't this make x unnecessary calls to the vector copy constructor? :/
Jul 19, 2010 at 9:00pm
Wander wrote:
I'm getting tons of weird errors with this:

This -> vector< vector<string> > mapCoords[x][y];

Should be -> vector< vector<string> > mapCoords;

Also, don't forget to #include <vector> and <string>
Last edited on Jul 19, 2010 at 9:04pm
Jul 19, 2010 at 9:10pm
@m4ster t0shi: WOAH!! it worked and all, but when it got to the for loop it said something about requesting the runtime to terminate in an unknown way and then it crashed.
Jul 19, 2010 at 9:10pm
m4ster r0shi wrote:
But... doesn't this make x unnecessary calls to the vector copy constructor? :/

Nope. It just makes x necessary calls to the copy constructor.
Last edited on Jul 19, 2010 at 9:11pm
Jul 19, 2010 at 9:15pm
Athar wrote:
Nope. It just makes x necessary calls to the copy constructor.

Why? You would use something like that if you already had a vector and wanted to create a vector of copies of that vector, right? Here, you don't care what the initial values will be, so all the element by element assignments it makes are unnecessary, no?
Last edited on Jul 19, 2010 at 9:16pm
Jul 19, 2010 at 9:21pm
@Wander:

Post your whole code.
Pages: 1234