By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,491 Members | 1,197 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,491 IT Pros & Developers. It's quick & easy.

2D Array using std::vector?

P: 100
I googled and got some answers but whenever I used it I kept getting and std::out_of_range exception and I'm not completely clear on how to use it.

I know how to initialize it; std::vector< std::vector<int> > 2DArray; but that's all I can do really :3

I'm not sure how you access data in the second vector or how you set it, having saying that I'm not completely sure how you set and access data for the first vector either?

Could someone provide an example or explanation please? :)
Aug 30 '08 #1
Share this Question
Share on Google+
5 Replies

P: 100
Ok, Here's what I've got so far:

Expand|Select|Wrap|Line Numbers
  1. Points = std::vector< std::vector<Vector3D> >(x+1, y+1);
  2.     Normals = std::vector< std::vector<Vector3D> >(x+1, y+1);
  4.     for(int i = 0; i < x+1; i++) {
  5.         std::vector<Vector3D> &Points_Temp =;
  6.         std::vector<Vector3D> &Normals_Temp =;
  7.         for(int j = 0; j < y+1; j++) {
  8.             Points_Temp.push_back(Vector3D(i, 0.0f, j));
  9.             Normals_Temp.push_back(Vector3D(0.0f, 1.0f, 0.0f));
  10.         }
  11.     }
Points and Normals are defined in the header file for the class as:
Expand|Select|Wrap|Line Numbers
  1. std::vector< std::vector<Vector3D> > Points;
  2. std::vector< std::vector<Vector3D> > Normals;
Vector3D just holds a x, y and z value and has a few functions.

What I have there doesn't work :( It's just filling Points with a bunch of Vector3Ds with values 0, 0, 0?
Aug 30 '08 #2

Expert 100+
P: 469
I'm not that familliar with vector syntax because I never use them, but I seem to have made your code work. This line:
Expand|Select|Wrap|Line Numbers
  1. std::vector< std::vector<Vector3D> >(x+1, y+1);
that second parameter, y+1, is filling your vector with zeroed out Vector3Ds. If you look into the vectors a little farther, you will see that you are actually adding those values to the vector, they just come after the zeros. Get rid of the second parameter. This works:
Expand|Select|Wrap|Line Numbers
  1. std::vector< std::vector<Vector3D> >(x+1);
Hope this helps.
Aug 30 '08 #3

Expert Mod 5K+
P: 9,197
A vector is required to be implemented as an array. Therefore, a vector of vectors is a 2D array:
Expand|Select|Wrap|Line Numbers
  1. vector<int>  v0;
  2.   vector<int>  v1;
  3.   vector<int>  v2;
  4.   v0.push_back(1);
  5.   v0.push_back(2);
  6.   v0.push_back(3);
  7.   v1.push_back(4);
  8.   v1.push_back(5);
  9.   v1.push_back(6);
  10.   v2.push_back(7);
  11.   v2.push_back(8);
  12.   v2.push_back(9);
  15.    vector< vector<int> > v;
  16.    v.push_back(v0);
  17.    v.push_back(v1);
  18.    v.push_back(v2);
  20.    cout << v[1][2] << endl;
You can load your individual vectors by using arrays rather than the push_back:
Expand|Select|Wrap|Line Numbers
  1. int arr[] = {1,2,3};
  2. vector<int> v0(arr, arr+3);
There is no reason in C++ to persist in using built-in arrays. Built-in arrays are in C++ mainly for backwards compatibility with C.
Aug 30 '08 #4

P: 100
Thank you, it works now :)

I thought doing (x+1, y+1) would make the first vector of size x+1 and the vector in that vector y+1, but obviously not :P

Also thank you for the explanation Weakness.
Aug 31 '08 #5

Expert 100+
P: 469
Glad it works. You could also keep the second parameter, filling your vector with default Vector3Ds, and then use assignment instead of push_back:
Expand|Select|Wrap|Line Numbers
  1. for(int j = 0; j < y+1; j++) {
  2.     Points_Temp[i][j] = Vector3D(i, 0.0f, j);
  3.     Normals_Temp[i][j] = Vector3D(0.0f, 1.0f, 0.0f);
  4. }
Aug 31 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.