Jump to content
  • Advertisement
Sign in to follow this  
utilae

2D Vector, how to add element

This topic is 4720 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I want to create a 2d vector, eg vector <vector<RECT*>> m_vecRectangles; I'm wondering how you use pushback to add an element using the dimensions, eg [3][1] if I wanted to add an element to the 3rd column and 1st row.

Share this post


Link to post
Share on other sites
Advertisement
does this work:

m_vecRectangles.at(3)(5).push_back(whatever);

if I wanted to put a RECT at location [3][5].

Share this post


Link to post
Share on other sites
Quote:
Hi, I want to create a 2d vector


That's not a 2D vector, that's a vector of vectors. Every row can have a different number of columns.

Quote:
Original post by utilae
does this work:

m_vecRectangles.at(3)(5).push_back(whatever);

if I wanted to put a RECT at location [3][5].


No.

What are you trying to do? To extend the vector or to modify an existing element at that location?

To grow the vector, you would do m_vecRectangles.at(3).push_back(whatever)

To modify the element, you would do m_vecRectangles.at(3).at(5) = whatever or m_vecRectangles[3][5] = whatever assuming your vectors already are big enough to accomodate an element there - otherwise you'd have to grow them first.

Share this post


Link to post
Share on other sites
Quote:
Original post by utilae
does this work:

m_vecRectangles.at(3)(5).push_back(whatever);

if I wanted to put a RECT at location [3][5].

It seems as though you don't have much experience with vectors, so lets start at the top.

First of all, avoid the RECT*. Just add RECT objects directly. The whole point behind using things like vectors is to avoid things like memory allocation. At the very least, use a smart pointer of some sort. Not an auto_ptr, though, because those aren't compatible with the library containers.

If you just want to tack a new object onto the end of a vector, you use .push_back(whatever). If you want to set an object in the middle, then you use vec.at(x) or vec[x]. The former is bounds checked, the latter is not.

Examples:

vector<int> vec1(5, 10); //creates a vector with 5 integers all set to 10
vector<int> vec2(5); //creates a vector with 5 integers [I think they equal 0, but I'm not sure they have to]
vector<int> vec3; //Creates an empty vector

vec1.push_back(8); //Adds a sixth element equal to 8, {10, 10, 10, 10, 10, 8}
cout << vec1[5] << endl; //prints 8
cout << vec1.at(5) << endl; //prints 8
cout << vec1[6] << endl; //Invalid element...possibly a buffer overflow
//[*edit: definately a buffer overflow, but possibly into memory the vector owns. Either way, it should be avoided]
cout << vec1.at(6) << endl; //Throws an exception...out_of_range, I believe

vec1.at(0) = 1; //Sets the first element, {1, 10, 10, 10, 10, 8}
vec1[1] = 2; //Sets the second element, {1, 2, 10, 10, 10, 8}



All that applies if the vector holds vectors.

struct rect
{
int x; int y; int z;
rect(int x_ = 0, int y_ = 0, int z_ = 0) : x(x_), y(y_), z(z_) {}
}

vector<vector<rect> > vec(5); //Creates five empty vector<rect> objects
vec.push_back(vector<rect>()); //Adds a sixth empty column
vec[0].push_back(vector<rect>(5)); //Adds a row of five default rect objects
vec[1].push_back(vector<rect>(1, rect(1, 2, 3))); //Adds a row of one rect object equal to {1, 2, 3}
vec[0] = vector<rect>(10); //Changes the first row to have 10 objects
vec.at(1)[0] = rect(1, 1, 1); //Sets the element [1][0] to {1, 1, 1}
vec[1].push_back(rect(2, 2, 2)); //Adds a second element to [1][0]
vec[1][1].x = 3; //Changes a member of element [1][1]

And so forth.

CM

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!