Multi-Dimensional Vectors

Pages: 1234
But you should keep in mind that resize() also default-constructs all new elements that are added.
So you don't really save anything... the only difference is that resize initializes all elements to zero and the copy constructor copies the zeroes over from the temporary vector. Same result, though.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main()
{
    int x, y;

    vector< vector<string> > mapCoords;

    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;
}
Athar wrote:
the copy constructor copies the zeroes over from the temporary vector

Ah, which means that I save the temporary vector access time ;)

@Wander:

You forgot to initialize x and y.
@m4ster r0shi: I did initialize them: int x, y;
No, you didn't :P You just declared them. Initialization means to declare them and give them some value.

Try

1
2
cout << x << endl;
cout << y << endl;

after your declarations and see what their values are.
WOAH!! those are some crazy values!! hahaha. i thought ints were automatically declared as 0s :/
Global ints (the ones you declare outside every function) are indeed initialized to zero when declared. But this rule doesn't apply to the ints you declare inside your functions (i.e. to ints that are automatic variables).
okay, well i initialized them to 0 and its still ending with a runtime error
..and again, post your whole code.
AAAAAAAA!!!!! I'm gonna bite you! Why would you want to initialize them to zero? Give them the values of the dimensions of your array. For example 100,100 or 500,1000 or, in your case, 3000,3000 (which will result to a total of 9000000 elements)
LOL!!!! ROFL!!!!!!! i changed it to 3000 and that didnt fix my problem:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#define MAP_RANGE 2999

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main()
{
    int x, y = 3000;

    vector< vector<string> > mapCoords;

    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;
}
Ouuuch... and what about x?
Athar, would you do us the honor to explain what is wrong here, please?

EDIT: Oh, ok, I guess you just did :P
Last edited on
wut about it?
OH DANG!!! HOW COULD I BE SO STUPID!!!!!! LOL. now it works :)
Last edited on
Thank you guys very very much. :) Now, I just gotta figure out how to use it. :/
Here's a small example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main()
{
    int x=2, y=3;
    int i,j;
    string temp;

    vector< vector<string> > str_array2d;

    str_array2d.resize(x);

    for (i=0; i<x; i++)
        str_array2d[i].resize(y);

    cout << "let's set the array...\n" << endl;
    for (i=0; i<x; i++)
    {
        for (j=0; j<y; j++)
        {
            cout << "enter string at [";
            cout << i << "][" << j << "]: ";
            getline(cin,temp);

            //so simple!
            str_array2d[i][j]=temp;
        }
    }

    cout << "\nnow let's print it!\n" << endl;
    for (i=0; i<x; i++)
    {
        for (j=0; j<y; j++)
        {
            cout << "string at [";
            cout << i << "][" << j << "]: ";
            cout << str_array2d[i][j] << endl;
        }
    }

    cout << "\nhit enter to quit...";
    cin.get();
    return 0;
}
Okay I understand some of this, but what exactly is this supposed to do?

And are system("pause)/("cls") bad to use?
Wander wrote:
what exactly is this supposed to do?

Did you run it? It's just a demonstration of how you can use the array in your program.

Wander wrote:
And are system("pause)/("cls") bad to use?

http://cplusplus.com/forum/articles/11153/
Yeah I ran it. So I gotta use all of those dern nested for loops to enter stuff into the vector<vector>
Pages: 1234