473,657 Members | 2,727 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Stumpted: sorting vectors.

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.

Aug 24 '06 #1
13 2543
"JoeC" <en*****@yahoo. comwrote in message
news:11******** **************@ i42g2000cwa.goo glegroups.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?
Aug 24 '06 #2
In article <11************ **********@i42g 2000cwa.googleg roups.com>,
"JoeC" <en*****@yahoo. comwrote:
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 );
Aug 24 '06 #3

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.b egin(), v.end()), v.end());

where v is the sorted vector of objects.

Greg

Aug 24 '06 #4

Jim Langston wrote:
"JoeC" <en*****@yahoo. comwrote in message
news:11******** **************@ i42g2000cwa.goo glegroups.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,8) (3,6)

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

Aug 24 '06 #5

"JoeC" <en*****@yahoo. comwrote in message
news:11******** **************@ b28g2000cwb.goo glegroups.com.. .
>
Jim Langston wrote:
>"JoeC" <en*****@yahoo. comwrote in message
news:11******* *************** @i42g2000cwa.go oglegroups.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,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 <0and <1represent? 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

Aug 24 '06 #6

Howard wrote:
"JoeC" <en*****@yahoo. comwrote in message
news:11******** **************@ b28g2000cwb.goo glegroups.com.. .

Jim Langston wrote:
"JoeC" <en*****@yahoo. comwrote in message
news:11******** **************@ i42g2000cwa.goo glegroups.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,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 <0and <1represent? 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.

Aug 24 '06 #7

"JoeC" <en*****@yahoo. comwrote in message
news:11******** **************@ i3g2000cwc.goog legroups.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,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


Aug 24 '06 #8

Howard wrote:
"JoeC" <en*****@yahoo. comwrote in message
news:11******** **************@ i3g2000cwc.goog legroups.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,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.

Aug 24 '06 #9
JoeC <en*****@yahoo. comwrote:
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<c oordinate, pieceto 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
Aug 24 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3410
by: Pratyush | last post by:
Hi, Suppose there is a vector of objects of class A, i.e., std::vector<A> vec_A(N); The class A satisifies all the STL vector requirements. Now I wish to add some attributes for each of the objects in the vector vec_A. Suppose there are K attributes to be added. For each of the attributes I define K vectors of appropriate types. Say, the attributes have types type1, type2, ..., typeK. So I define std::vector<type1> attr1(vec_A.size());
2
2299
by: William Payne | last post by:
Hello, I have two structs: struct FileEntry { FileEntry(const char* name, const char* type, std::size_t file_size, HICON icon) : m_file_size(file_size),
18
3043
by: Matthias Kaeppler | last post by:
Hi, in my program, I have to sort containers of objects which can be 2000 items big in some cases. Since STL containers are based around copying and since I need to sort these containers quite frequently, I thought it'd be a better idea to manage additional containers which are initialized with pointers to the objects in the primary containers and sort those (only pointers have to be copied around then). However, that also means if I...
0
3335
by: hypernihl | last post by:
Hello, I have two vectors "vector<int> x,y;" I want to sort vector x "sort(x.begin(),x.end());" I want to sort vector y with respect to x. I need the new index of vector x after it has been sorted, so I can sort vector y the same way. Is it possible to keep track of the index of a vector. This is trivial with arrays. Thanx
20
4054
by: Xah Lee | last post by:
Sort a List Xah Lee, 200510 In this page, we show how to sort a list in Python & Perl and also discuss some math of sort. To sort a list in Python, use the “sort” method. For example: li=;
9
2831
by: Daz | last post by:
Hello people! (This post is best viewed using a monospace font). I need to create a class, which holds 4 elements: std::string ItemName int Calories int Weight int Density
3
1887
by: Pino | last post by:
HI all, I am learning c++ ( just 1 month) and would want an aid for a code. I have a integer vector of length N. ie 3 3 3 4 4 1
4
2040
by: jeromytheoutlaw | last post by:
/* Write a program that reads names and gpas from a text file. The file looks like: James 3.9 Margaret 3.5 Charles 1.2 Jennifer 4.0 Your program sorts the students ascending by gpa, and prints the sorted list including names. To keep the names with the numbers use parallel arrays, one for the names, the other for the numbers.
5
3762
by: xtheendx | last post by:
In my program i have three sperate vectors that store the students last name, first and score. I need to sort it by last name and print the contents of the vectors lastname, firstname: score. Well i have got it doing that for the most part. But after it sorts the lastnames the first names and scores are still in the same postion in their vectors, so when it prints the reults the last names are with the wrong first name and score. any ideas? ...
0
8394
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
8306
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7327
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4152
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1615
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.