473,396 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

2D Array using std::vector?

MrPickle
100 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
5 18376
MrPickle
100 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);
  3.  
  4.     for(int i = 0; i < x+1; i++) {
  5.         std::vector<Vector3D> &Points_Temp = Points.at(i);
  6.         std::vector<Vector3D> &Normals_Temp = Normals.at(i);
  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
boxfish
469 Expert 256MB
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
  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
weaknessforcats
9,208 Expert Mod 8TB
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);
  13.  
  14.  
  15.    vector< vector<int> > v;
  16.    v.push_back(v0);
  17.    v.push_back(v1);
  18.    v.push_back(v2);
  19.  
  20.    cout << v[1][2] << endl;
  21.  
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);
  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
MrPickle
100 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
boxfish
469 Expert 256MB
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

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

Similar topics

8
by: Hagen | last post by:
Hi, I have a question that you probably shouldn´t worry about since the compiler cares for it, but anyways: When you run your compiler with optimization turned on (eg. g++ with -Ox flag) and...
20
by: Anonymous | last post by:
Is there a non-brute force method of doing this? transform() looked likely but had no predefined function object. std::vector<double> src; std::vector<int> dest; ...
17
by: Michael Hopkins | last post by:
Hi all I want to create a std::vector that goes from 1 to n instead of 0 to n-1. The only change this will have is in loops and when the vector returns positions of elements etc. I am calling...
2
by: Eric | last post by:
I am converting my VC++ MFC to Manage C++ and I am having trouble using the vector class. Here is my __gc class ....other headers for System classes .... #include <algorithm> #include...
8
by: Jason Heyes | last post by:
Does the STL have a function like this one? template <typename T> void remove(std::vector<T> &v, std::vector<T>::size_type index) { std::swap(v, v.back()); v.resize(index); } Unlike...
56
by: Peter Olcott | last post by:
I am trying to refer to the same std::vector in a class by two different names, I tried a union, and I tried a reference, I can't seem to get the syntax right. Can anyone please help? Thanks
8
by: Lionel B | last post by:
On my platform I find that the std::vector<boolspecialisation incurs a significant performance hit in some circumstances (when compared, say, to std::vector<intprogrammed analagously). Is it...
4
by: kungfuelmosan | last post by:
Hey guys, Im just getting into c++ at the moment so please bare with me Basically i need to declare a vector<stringstringArray(50) inside a class, however by doing so i am getting the following...
3
by: vrsathyan | last post by:
Hi.., While executing the following code in purifier.., std::vector<int> vecX; vecX.clear(); int iCount = 0; { int iVal;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.