Velocity Reviews > C++ > Stumpted: sorting vectors.

# Stumpted: sorting vectors.

JoeC
Guest
Posts: n/a

 08-24-2006
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.

Jim Langston
Guest
Posts: n/a

 08-24-2006
"JoeC" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
>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?

Daniel T.
Guest
Posts: n/a

 08-24-2006
In article <(E-Mail Removed) .com>,
"JoeC" <(E-Mail Removed)> 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 );

Greg
Guest
Posts: n/a

 08-24-2006

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

JoeC
Guest
Posts: n/a

 08-24-2006

Jim Langston wrote:
> "JoeC" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) oups.com...
> >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, (3,6)

sorted
<0>
l1 l2
u1 u3
u3
<1>
u2 u5

Howard
Guest
Posts: n/a

 08-24-2006

"JoeC" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
>
> Jim Langston wrote:
>> "JoeC" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed) oups.com...
>> >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, (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...?

Are they just coordinates (a couple of ints, for example)? Or are those
"u1", etc., labels part of the structures somehow as well?

-Howard

JoeC
Guest
Posts: n/a

 08-24-2006

Howard wrote:
> "JoeC" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) oups.com...
> >
> > Jim Langston wrote:
> >> "JoeC" <(E-Mail Removed)> wrote in message
> >> news:(E-Mail Removed) oups.com...
> >> >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, (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

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, (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.

Howard
Guest
Posts: n/a

 08-24-2006

"JoeC" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
>

> 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, (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

JoeC
Guest
Posts: n/a

 08-24-2006

Howard wrote:
> "JoeC" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) ups.com...
> >

>
> > 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, (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.

Marcus Kwok
Guest
Posts: n/a

 08-24-2006
JoeC <(E-Mail Removed)> 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