Stumpted: sorting vectors.

J

JoeC

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.
 
J

Jim Langston

JoeC said:
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?
 
D

Daniel T.

"JoeC said:
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 );
 
G

Greg

JoeC said:
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
 
J

JoeC

Jim said:
"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
 
H

Howard

JoeC said:
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
 
J

JoeC

Howard said:
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.
list 1 2
 
H

Howard

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

list 1 2
----------------------

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
 
J

JoeC

Howard said:
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.
 
M

Marcus Kwok

JoeC said:
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.
 
J

JoeC

Greg said:
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.
 
J

JoeC

Marcus said:
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.

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 ++){

}

}
 
J

JoeC

Marcus said:
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.

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 ++){

}

}
 
J

JoeC

Marcus said:
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.

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 ++){

}

}
 

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

Staff online

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top