using vector or other classes to store vector coordinates

Discussion in 'C++' started by ssylee, Jan 13, 2009.

  1. ssylee

    ssylee Guest

    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?
     
    ssylee, Jan 13, 2009
    #1
    1. Advertising

  2. On Jan 13, 3:50 pm, ssylee <> wrote:
    > 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'.
     
    Zachary Turner, Jan 13, 2009
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tony Johansson

    Nested classes within other classes

    Tony Johansson, Dec 14, 2004, in forum: C++
    Replies:
    2
    Views:
    495
    msalters
    Dec 14, 2004
  2. pmatos
    Replies:
    6
    Views:
    24,028
  3. Suresh  Kumar
    Replies:
    0
    Views:
    612
    Suresh Kumar
    Jul 3, 2003
  4. Suresh  Kumar
    Replies:
    0
    Views:
    525
    Suresh Kumar
    Jul 4, 2003
  5. Replies:
    8
    Views:
    1,985
    Csaba
    Feb 18, 2006
Loading...

Share This Page