459,491 Members | 1,197 Online
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?

 100+ 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 > 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
5 Replies

 100+ P: 100 Ok, Here's what I've got so far: Expand|Select|Wrap|Line Numbers Points = std::vector< std::vector >(x+1, y+1);     Normals = std::vector< std::vector >(x+1, y+1);       for(int i = 0; i < x+1; i++) {         std::vector &Points_Temp = Points.at(i);         std::vector &Normals_Temp = Normals.at(i);         for(int j = 0; j < y+1; j++) {             Points_Temp.push_back(Vector3D(i, 0.0f, j));             Normals_Temp.push_back(Vector3D(0.0f, 1.0f, 0.0f));         }     } Points and Normals are defined in the header file for the class as: Expand|Select|Wrap|Line Numbers std::vector< std::vector > Points; std::vector< std::vector > 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 Hi, 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 std::vector< std::vector >(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 std::vector< std::vector >(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 vector  v0;   vector  v1;   vector  v2;   v0.push_back(1);   v0.push_back(2);   v0.push_back(3);   v1.push_back(4);   v1.push_back(5);   v1.push_back(6);   v2.push_back(7);   v2.push_back(8);   v2.push_back(9);        vector< vector > v;    v.push_back(v0);    v.push_back(v1);    v.push_back(v2);      cout << v[1][2] << endl;   You can load your individual vectors by using arrays rather than the push_back: Expand|Select|Wrap|Line Numbers int arr[] = {1,2,3}; vector 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

 100+ 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 for(int j = 0; j < y+1; j++) {     Points_Temp[i][j] = Vector3D(i, 0.0f, j);     Normals_Temp[i][j] = Vector3D(0.0f, 1.0f, 0.0f); } Aug 31 '08 #6