Multi-Dimensional Vectors

Pages: 1234
Jul 19, 2010 at 9:23pm
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.
Jul 19, 2010 at 9:23pm
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;
}
Jul 19, 2010 at 9:31pm
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.
Jul 19, 2010 at 9:34pm
@m4ster r0shi: I did initialize them: int x, y;
Jul 19, 2010 at 9:36pm
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.
Jul 19, 2010 at 9:39pm
WOAH!! those are some crazy values!! hahaha. i thought ints were automatically declared as 0s :/
Jul 19, 2010 at 9:42pm
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).
Jul 19, 2010 at 9:43pm
okay, well i initialized them to 0 and its still ending with a runtime error
Jul 19, 2010 at 9:47pm
..and again, post your whole code.
Jul 19, 2010 at 9:48pm
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)
Jul 19, 2010 at 9:50pm
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;
}
Jul 19, 2010 at 9:51pm
Ouuuch... and what about x?
Jul 19, 2010 at 9:51pm
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 Jul 19, 2010 at 9:52pm
Jul 19, 2010 at 9:51pm
wut about it?
Jul 19, 2010 at 9:53pm
OH DANG!!! HOW COULD I BE SO STUPID!!!!!! LOL. now it works :)
Last edited on Jul 19, 2010 at 9:55pm
Jul 19, 2010 at 10:02pm
Thank you guys very very much. :) Now, I just gotta figure out how to use it. :/
Jul 19, 2010 at 10:12pm
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;
}
Jul 19, 2010 at 10:17pm
Okay I understand some of this, but what exactly is this supposed to do?

And are system("pause)/("cls") bad to use?
Jul 19, 2010 at 10:31pm
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/
Jul 19, 2010 at 10:38pm
Yeah I ran it. So I gotta use all of those dern nested for loops to enter stuff into the vector<vector>
Pages: 1234