Jump to content

Creating a Two Dimensional Vector

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
whitey6993

whitey6993

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 437 posts
This tutorial will show you how to create a vector matrix, much like an array matrix, for storing data in a two dimensional format. An knowledge of arrays, vectors, loops, and basic C++ programming is necessary to fully grasp the material covered in this tutorial.

If you have dealt in array's for a while, you have probably come across two dimensional arrays (matrix). These arrays are often used for storing information in tables or anything where you would want to have two dimensions for data. Since matrices have been so helpful in arrays, making a two dimensional vector would be even better. However, there are a few tricks to making the two dimensional vector.

Declaring a two dimensional vector is similar to declaring an array. To start off, you declare your vector of whatever type you want. Use the following code to declare your vector:


vector<vector <int> > vec;

You might think that you can fill in the vectors right now, much like you would an array matrix, but one more step must be taken. Because there are no dimensions assigned for the vector matrix right now, the rows of the matrix don't actually exist yet. What we have after the code we just typed in is an empty vector that can hold vectors of integers. To create the rows of the vector, we have to use a loop like the following:


for(int i = 0; i < 5; i++)

{

    vector<int> row;

    vec.push_back(row);

}

This statement will fill the vector "vec" with empty vectors that can contain integers. Now, we can use an assignment statement to fill a part of the matrix:


vec[0].push_back(5);

And, as you can see by outputting the part of the matrix that was filled, the data was correctly assigned:


cout << vec[0][0] << endl;

Tada! You have now created a two dimensional vector.

This code was written and compiled in Dev-C++ and may need some tweaking to run in other compilers. The source code for this project is attached for convenience. If you would like to offer corrections or contribute to the information presented here, please post a reply.

Attached Files



#2
outsid3r

outsid3r

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 623 posts
In my opinion this example isn't good. You're trying to demonstrate a dynamic multi-dimentional array, for what? To start, i see no usefulness in your example, and it's a little non-sense to me, nobody would implement a dynamic multi-dimentional array like that, because we can use a single vector and encapsulate it within a class and then "emulate" multi-dimentional assignment, for example, a map class for a game, which would have a vector as an attribute of the class, and methods like get_at() and set_at() with x and y parameters, but of course that it would require some simple math in order to "emulate" multi-dimentional in a single vector.
In your example, you also show inefficiency and redundancy, because each row would contain data that is present in all rows, that is not needed, like the size of the row, etc etc, and it's a waste of memory because you don't need that information in each row.