Stumpted: sorting vectors.

Discussion in 'C++' started by JoeC, Aug 24, 2006.

  1. JoeC

    JoeC Guest

    I am completly lost.

    I would like to create a function that takes two vectors.
    These two vectors have objects with x and y coords, what I want to do
    find all the objects in the same x and y coords and put all the objects
    in the same coordinate together.
     
    JoeC, Aug 24, 2006
    #1
    1. Advertising

  2. JoeC

    Jim Langston Guest

    "JoeC" <> wrote in message
    news:...
    >I am completly lost.
    >
    > I would like to create a function that takes two vectors.
    > These two vectors have objects with x and y coords, what I want to do
    > find all the objects in the same x and y coords and put all the objects
    > in the same coordinate together.


    "put all objects in the same coordinate together." What does that mean?
     
    Jim Langston, Aug 24, 2006
    #2
    1. Advertising

  3. JoeC

    Daniel T. Guest

    In article <>,
    "JoeC" <> wrote:

    > I am completly lost.
    >
    > I would like to create a function that takes two vectors.
    > These two vectors have objects with x and y coords, what I want to do
    > find all the objects in the same x and y coords and put all the objects
    > in the same coordinate together.


    First write a comparison function that tells when one coord is "less
    than" another. Then:

    sort( vec.begin(), vec.end(), &my_compare );
     
    Daniel T., Aug 24, 2006
    #3
  4. JoeC

    Greg Guest

    JoeC wrote:
    > I am completly lost.
    >
    > I would like to create a function that takes two vectors.
    > These two vectors have objects with x and y coords, what I want to do
    > find all the objects in the same x and y coords and put all the objects
    > in the same coordinate together.


    It sounds like you want to remove duplicates - if so, copy the objects
    into a single vector, sort them, and then erase the redundant objects
    like so:

    v.erase( std::unique(v.begin(), v.end()), v.end());

    where v is the sorted vector of objects.

    Greg
     
    Greg, Aug 24, 2006
    #4
  5. JoeC

    JoeC Guest

    Jim Langston wrote:
    > "JoeC" <> wrote in message
    > news:...
    > >I am completly lost.
    > >
    > > I would like to create a function that takes two vectors.
    > > These two vectors have objects with x and y coords, what I want to do
    > > find all the objects in the same x and y coords and put all the objects
    > > in the same coordinate together.

    >
    > "put all objects in the same coordinate together." What does that mean?

    u1 u2 u3 u4 u5
    reds (2,4) (3,6) (2,4) (5,5) (8,3)
    blue (2.4) (5,5) (4,7) (6,8) (3,6)

    sorted
    <0>
    l1 l2
    u1 u3
    u3
    <1>
    u2 u5
     
    JoeC, Aug 24, 2006
    #5
  6. JoeC

    Howard Guest

    "JoeC" <> wrote in message
    news:...
    >
    > Jim Langston wrote:
    >> "JoeC" <> wrote in message
    >> news:...
    >> >I am completly lost.
    >> >
    >> > I would like to create a function that takes two vectors.
    >> > These two vectors have objects with x and y coords, what I want to do
    >> > find all the objects in the same x and y coords and put all the objects
    >> > in the same coordinate together.

    >>
    >> "put all objects in the same coordinate together." What does that mean?

    > u1 u2 u3 u4 u5
    > reds (2,4) (3,6) (2,4) (5,5) (8,3)
    > blue (2.4) (5,5) (4,7) (6,8) (3,6)
    >
    > sorted
    > <0>
    > l1 l2
    > u1 u3
    > u3
    > <1>
    > u2 u5
    >


    That's even more confusing than the question! What's "l1" and "l2"? What
    are "u1" through "u5"? I can guess that the "reds" and "blues" are your two
    original vectors, but the arrangement of what you call "sorted" beneath that
    makes no sense to me. What do the two sections <0> and <1> represent? And
    what do the multiple columns and rows represent? That looks like some kind
    of matrix, but I don't know what it means.

    What, specifically, do you want as a result? One new vector? The same two
    vectors, but sorted by some criteria? Or...?

    When you say you want similar items "together", what does that mean? Next
    to each other in a vector? Combined into one new type of object which you
    haven't described, and that object put in a vector and sorted? Or...?

    Perhaps seeing some actual code showing your class(es) might be helpful.
    Are they just coordinates (a couple of ints, for example)? Or are those
    "u1", etc., labels part of the structures somehow as well?

    -Howard
     
    Howard, Aug 24, 2006
    #6
  7. JoeC

    JoeC Guest

    Howard wrote:
    > "JoeC" <> wrote in message
    > news:...
    > >
    > > Jim Langston wrote:
    > >> "JoeC" <> wrote in message
    > >> news:...
    > >> >I am completly lost.
    > >> >
    > >> > I would like to create a function that takes two vectors.
    > >> > These two vectors have objects with x and y coords, what I want to do
    > >> > find all the objects in the same x and y coords and put all the objects
    > >> > in the same coordinate together.
    > >>
    > >> "put all objects in the same coordinate together." What does that mean?

    > > u1 u2 u3 u4 u5
    > > reds (2,4) (3,6) (2,4) (5,5) (8,3)
    > > blue (2.4) (5,5) (4,7) (6,8) (3,6)
    > >
    > > sorted
    > > <0>
    > > l1 l2
    > > u1 u3
    > > u3
    > > <1>
    > > u2 u5


    > >

    >
    > That's even more confusing than the question! What's "l1" and "l2"? What
    > are "u1" through "u5"? I can guess that the "reds" and "blues" are your two
    > original vectors, but the arrangement of what you call "sorted" beneath that
    > makes no sense to me. What do the two sections <0> and <1> represent? And
    > what do the multiple columns and rows represent? That looks like some kind
    > of matrix, but I don't know what it means.
    >
    > What, specifically, do you want as a result? One new vector? The same two
    > vectors, but sorted by some criteria? Or...?
    >
    > When you say you want similar items "together", what does that mean? Next
    > to each other in a vector? Combined into one new type of object which you
    > haven't described, and that object put in a vector and sorted? Or...?
    >
    > Perhaps seeing some actual code showing your class(es) might be helpful.
    > Are they just coordinates (a couple of ints, for example)? Or are those
    > "u1", etc., labels part of the structures somehow as well?
    >
    > -Howard


    I see your point...
    Basically I am trying to group pieces that are in the same space for a
    combat routine.

    > > u1 u2 u3 u4 u5
    > > reds (2,4) (3,6) (2,4) (5,5) (8,3) red side
    > > blue (2.4) (5,5) (4,7) (6,8) (3,6) blue side.
    > >
    > > sorted

    list 1 2
    ----------------------
    > > <0> <-- This is the first instance where pieces are in the same space.
    > > u1 u3 <- units from the arrays
    > > u3
    > > <1> <-- a different space that contatins both sides.
    > > u2 u5


    I hope this helps. I want to find out what units are in the same space
    so they attack factors and defence can be accumilated and the units can
    have combat.
     
    JoeC, Aug 24, 2006
    #7
  8. JoeC

    Howard Guest

    "JoeC" <> wrote in message
    news:...
    >


    > Basically I am trying to group pieces that are in the same space for a
    > combat routine.
    >
    >> > u1 u2 u3 u4 u5
    >> > reds (2,4) (3,6) (2,4) (5,5) (8,3) red side
    >> > blue (2.4) (5,5) (4,7) (6,8) (3,6) blue side.
    >> >
    >> > sorted

    > list 1 2
    > ----------------------
    >> > <0> <-- This is the first instance where pieces are in the same space.
    >> > u1 u3 <- units from the arrays
    >> > u3
    >> > <1> <-- a different space that contatins both sides.
    >> > u2 u5

    >
    > I hope this helps. I want to find out what units are in the same space
    > so they attack factors and defence can be accumilated and the units can
    > have combat.
    >


    I'm still not sure I understand what that diagram represents, as far as a
    data structure is concerned.

    Perhaps, though, instead of visualizing some kind of grouping as you appear
    to be doing, you might consider a 2D array representing the spaces
    themselves, where the contents of each cell (space) in the array are a list
    (or pair of lists, red and blue) of the units currently in that space. So,
    for the example above, the cell at (2,4) would contain a list (or two)
    consisting of two red units and one blue unit.

    You could store pointers to the actual units in those lists, or you could
    store some kind of identifiers or indexes so that you can go look them up in
    the red and blue lists. Then you just use a double-loop through the 2D
    matrix, resolving conflicts for each space separately. If your coordinate
    space isn't toooo big, this would work pretty well, I think.

    -Howard
     
    Howard, Aug 24, 2006
    #8
  9. JoeC

    JoeC Guest

    Howard wrote:
    > "JoeC" <> wrote in message
    > news:...
    > >

    >
    > > Basically I am trying to group pieces that are in the same space for a
    > > combat routine.
    > >
    > >> > u1 u2 u3 u4 u5
    > >> > reds (2,4) (3,6) (2,4) (5,5) (8,3) red side
    > >> > blue (2.4) (5,5) (4,7) (6,8) (3,6) blue side.
    > >> >
    > >> > sorted

    > > list 1 2
    > > ----------------------
    > >> > <0> <-- This is the first instance where pieces are in the same space.
    > >> > u1 u3 <- units from the arrays
    > >> > u3
    > >> > <1> <-- a different space that contatins both sides.
    > >> > u2 u5

    > >
    > > I hope this helps. I want to find out what units are in the same space
    > > so they attack factors and defence can be accumilated and the units can
    > > have combat.
    > >

    >
    > I'm still not sure I understand what that diagram represents, as far as a
    > data structure is concerned.
    >
    > Perhaps, though, instead of visualizing some kind of grouping as you appear
    > to be doing, you might consider a 2D array representing the spaces
    > themselves, where the contents of each cell (space) in the array are a list
    > (or pair of lists, red and blue) of the units currently in that space. So,
    > for the example above, the cell at (2,4) would contain a list (or two)
    > consisting of two red units and one blue unit.
    >
    > You could store pointers to the actual units in those lists, or you could
    > store some kind of identifiers or indexes so that you can go look them up in
    > the red and blue lists. Then you just use a double-loop through the 2D
    > matrix, resolving conflicts for each space separately. If your coordinate
    > space isn't toooo big, this would work pretty well, I think.
    >
    > -Howard


    I did do that for a different game and that became very cumbersome and
    buggy with all the pointers and taking the piece out of a space and
    putting them in another space. For this project my pieces store their
    x and y location and they draw themselvs where they should be on the
    board. Simply what I am trying to do is have pieces in the same space
    fight each otehr if they are on different sides.
     
    JoeC, Aug 24, 2006
    #9
  10. JoeC

    Marcus Kwok Guest

    JoeC <> wrote:
    > For this project my pieces store their
    > x and y location and they draw themselvs where they should be on the
    > board. Simply what I am trying to do is have pieces in the same space
    > fight each otehr if they are on different sides.


    I just had an idea. Maybe you could build a
    std::multimap<coordinate, piece> to find which pieces are in the same
    location. Erasing items from the multimap might be a little tricky, so
    if the performance isn't too bad (you will have to profile it to see) it
    may be easier to just completely rebuild the multimap after any piece
    moves.

    --
    Marcus Kwok
    Replace 'invalid' with 'net' to reply
     
    Marcus Kwok, Aug 24, 2006
    #10
  11. JoeC

    JoeC Guest

    Greg wrote:
    > JoeC wrote:
    > > I am completly lost.
    > >
    > > I would like to create a function that takes two vectors.
    > > These two vectors have objects with x and y coords, what I want to do
    > > find all the objects in the same x and y coords and put all the objects
    > > in the same coordinate together.

    >
    > It sounds like you want to remove duplicates - if so, copy the objects
    > into a single vector, sort them, and then erase the redundant objects
    > like so:
    >
    > v.erase( std::unique(v.begin(), v.end()), v.end());
    >
    > where v is the sorted vector of objects.
    >
    > Greg


    I wish I knew more about algorithms. Still I don't want any duplicate
    pieces removed.
     
    JoeC, Aug 24, 2006
    #11
  12. JoeC

    JoeC Guest

    Marcus Kwok wrote:
    > JoeC <> wrote:
    > > For this project my pieces store their
    > > x and y location and they draw themselvs where they should be on the
    > > board. Simply what I am trying to do is have pieces in the same space
    > > fight each otehr if they are on different sides.

    >
    > I just had an idea. Maybe you could build a
    > std::multimap<coordinate, piece> to find which pieces are in the same
    > location. Erasing items from the multimap might be a little tricky, so
    > if the performance isn't too bad (you will have to profile it to see) it
    > may be easier to just completely rebuild the multimap after any piece
    > moves.
    >
    > --
    > Marcus Kwok
    > Replace 'invalid' with 'net' to reply


    I think I solved it. It was a challlenge. At least it dosn't crash:

    void fight(vector<unit>& at, vector<unit>& dt,
    board * b , terrain * trn ){

    vector<fightclass> fighting;
    for(int atu = 0; atu != at.size(); atu++){
    for(int lp = 0; lp != fighting.size(); lp++){
    if(at[atu].getXloc() == fighting[lp].getX() &&
    at[atu].getYloc() == fighting[lp].getY()){
    fighting[lp].addAu(at[atu]);
    } else {
    fightclass ft;
    ft.addAu(at[atu]);
    fighting.push_back(ft);
    }
    }
    }
    for(int dtu = 0; dtu != dt.size(); dtu++){
    for(int lp = 0; lp != fighting.size(); lp++){
    if(dt[dtu].getXloc() == fighting[lp].getX() &&
    dt[dtu].getYloc() == fighting[lp].getY()){
    fighting[lp].addDu(dt[dtu]);
    }
    }
    }
    for(int lp = 0; lp != fighting.size(); lp ++){

    }

    }
     
    JoeC, Aug 24, 2006
    #12
  13. JoeC

    JoeC Guest

    Marcus Kwok wrote:
    > JoeC <> wrote:
    > > For this project my pieces store their
    > > x and y location and they draw themselvs where they should be on the
    > > board. Simply what I am trying to do is have pieces in the same space
    > > fight each otehr if they are on different sides.

    >
    > I just had an idea. Maybe you could build a
    > std::multimap<coordinate, piece> to find which pieces are in the same
    > location. Erasing items from the multimap might be a little tricky, so
    > if the performance isn't too bad (you will have to profile it to see) it
    > may be easier to just completely rebuild the multimap after any piece
    > moves.
    >
    > --
    > Marcus Kwok
    > Replace 'invalid' with 'net' to reply


    I think I solved it. It was a challlenge. At least it dosn't crash:

    void fight(vector<unit>& at, vector<unit>& dt,
    board * b , terrain * trn ){

    vector<fightclass> fighting;
    for(int atu = 0; atu != at.size(); atu++){
    for(int lp = 0; lp != fighting.size(); lp++){
    if(at[atu].getXloc() == fighting[lp].getX() &&
    at[atu].getYloc() == fighting[lp].getY()){
    fighting[lp].addAu(at[atu]);
    } else {
    fightclass ft;
    ft.addAu(at[atu]);
    fighting.push_back(ft);
    }
    }
    }
    for(int dtu = 0; dtu != dt.size(); dtu++){
    for(int lp = 0; lp != fighting.size(); lp++){
    if(dt[dtu].getXloc() == fighting[lp].getX() &&
    dt[dtu].getYloc() == fighting[lp].getY()){
    fighting[lp].addDu(dt[dtu]);
    }
    }
    }
    for(int lp = 0; lp != fighting.size(); lp ++){

    }

    }
     
    JoeC, Aug 24, 2006
    #13
  14. JoeC

    JoeC Guest

    Marcus Kwok wrote:
    > JoeC <> wrote:
    > > For this project my pieces store their
    > > x and y location and they draw themselvs where they should be on the
    > > board. Simply what I am trying to do is have pieces in the same space
    > > fight each otehr if they are on different sides.

    >
    > I just had an idea. Maybe you could build a
    > std::multimap<coordinate, piece> to find which pieces are in the same
    > location. Erasing items from the multimap might be a little tricky, so
    > if the performance isn't too bad (you will have to profile it to see) it
    > may be easier to just completely rebuild the multimap after any piece
    > moves.
    >
    > --
    > Marcus Kwok
    > Replace 'invalid' with 'net' to reply


    I think I solved it. It was a challlenge. At least it dosn't crash:

    void fight(vector<unit>& at, vector<unit>& dt,
    board * b , terrain * trn ){

    vector<fightclass> fighting;
    for(int atu = 0; atu != at.size(); atu++){
    for(int lp = 0; lp != fighting.size(); lp++){
    if(at[atu].getXloc() == fighting[lp].getX() &&
    at[atu].getYloc() == fighting[lp].getY()){
    fighting[lp].addAu(at[atu]);
    } else {
    fightclass ft;
    ft.addAu(at[atu]);
    fighting.push_back(ft);
    }
    }
    }
    for(int dtu = 0; dtu != dt.size(); dtu++){
    for(int lp = 0; lp != fighting.size(); lp++){
    if(dt[dtu].getXloc() == fighting[lp].getX() &&
    dt[dtu].getYloc() == fighting[lp].getY()){
    fighting[lp].addDu(dt[dtu]);
    }
    }
    }
    for(int lp = 0; lp != fighting.size(); lp ++){

    }

    }
     
    JoeC, Aug 24, 2006
    #14
    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. Richard Anderson

    sorting vectors with Date objects

    Richard Anderson, Dec 16, 2003, in forum: Java
    Replies:
    7
    Views:
    1,034
    John C. Bollinger
    Dec 16, 2003
  2. Pratyush
    Replies:
    5
    Views:
    346
    tom_usenet
    Nov 11, 2003
  3. Replies:
    3
    Views:
    709
    Shadowman
    Mar 26, 2008
  4. Replies:
    2
    Views:
    1,367
    Marcel Müller
    Jan 4, 2009
  5. Guest
    Replies:
    0
    Views:
    461
    Guest
    Sep 14, 2005
Loading...

Share This Page