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.
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 ).
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
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
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.
@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.
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?