using vector or other classes to store vector coordinates

S

ssylee

What would be the easiest way to store a series of vector coordinates
inside a data structure like stl vectors or other stl structures if I
don't want to deal with dynamic memory allocation with 2D arrays
myself?
 
Z

Zachary Turner

What would be the easiest way to store a series of vector coordinates
inside a data structure like stl vectors or other stl structures if I
don't want to deal with dynamic memory allocation with 2D arrays
myself?

Create a struct called vector. E.g.

struct Vector2D { double x; double y; }

Then create a vector of Vector2Ds.

std::vector<Vector2D> coords;

Of course, stl provides a class called pair if it actually turns out
you only need a 2-dimensional vectors. So, you could do:

using namespace std;
vector<pair<double> > coords;
coords.push_back(make_pair(1.0, 2.0));
coords.push_back(make_pair(3.0, 4.0));


I suspect this is not the case, but if you want to store coordinates
of differing dimensions, like for example maybe the first item is a 2d
coord vector, and the second item is a 3d coord vector, you can do
something like this:

vector<vector<double> > coords;
vector<double> temp;
temp.push_back(1.0); temp.push_back(2.0);
coords.push_back(temp);
temp.clear();
temp.push_back(1.0); temp.push_back(2.0); temp.push_back(3.0);
coords.push_back(temp);


First or second method is probably most suitable though. Note that
the code is identical if you want to use a list intead of a vector,
just change 'vector' in the type definition to 'list'.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top