nice idea and it does appear to be simple----I forgot to add my teacher wants us to use a bubble sort sorry for the confusion
Main Topics
Browse All TopicsI've got a simple program using 2 vectors. string<vector>Vcar the other is int<vector>Vmpg
This is a homework assignment---I have to sort the string vector which I can do but the int vector does not sort. Is there a SIMPLE solution (I'm a student) which will keep the int<vector> with the string<vector> associated. I know this isn't the best way to do this arrays would be more effecient but this is the assignment. I'm not going to include the code because I don't want the literal answer.
Thanks in advance
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
>> I forgot to add my teacher wants us to use a bubble sort sorry for the confusion
Ok, so you should be able to implement a generic bubble sort routine, which will sort anything that implements or supports a operator <(). It shouldn't matter what's being sorted as the semantics are the same. So in your case the logic for sorting int types is identical to sorting strings, it's just the predicate for sorting (the operator < ()) that is specific for the type. In the case of strings you normally perform a lexical comparison and with int types its a value comparison.
If you already have a bubble sort written that works correctly it should be pretty simple to modify it to work with int types, either by copying it and changing as necessary or (the smarter way) making it templated to work with any type.
I take it you know how a bubble sort works?
What, specifically, are you stuck on? Can you provide the problem code please for review?
Thanks.
how to add to the bubble sort my other vector so it associates with the sorted vector In this case the string vector. I have a loose idea how bubble sorts work. I've got the code for the bubble sort but not how to add the other vector to it. in simple terms my program compares cars and their mpg.
one vector is the name of the car one vector is the mpg. So if I sort the car name how do I keep the mpg with the associated car
>> So if I sort the car name how do I keep the mpg with the associated car
Ah, I see now. Create a vector of a struct of type Car, where this struct stores both name and mpg. Sort the vector by name and the name and mpg will stay together since they are intrinsically bound together in the struct. Your struct should implement an operator<() that returns true if the lhs string is less than the rhs string for the name.
Something like this...
struct Car
{
int mpg;
string name;
bool operator <(string lhs, string rhs) { return true if string lhs < string rhs }
}
vector <Car> vc;
// Add cars to vc
bubble_sort(vc);
This is far simpler than trying to associate 2 vectors and maintain ordering.
Thanks I'll accept your solution but this is not what my teacher was looking for
I'm aware my original issue was not a very good way to store data but we haven't gotten to arrays yet
My program has been written for both vectors to be used as separate entities after the sort
I'll keep looking for another solution
I appreciate your help
If you're just trying to keep the vectors associated, simply do the exact same manipulations on the 2nd vector.
Of course the attached code is meaingless. But the point is you're only comparing your string vector, and doing the same sorting manipulation with your int vector but not comparing it. That way the indices get changed in the same way as each other.
>>>> If you're just trying to keep the vectors associated, simply do the exact same manipulations on the 2nd vector.
That means if you swap strings of the string vector - say swap at positions i and j - then swap at the same positions in the int vector.
Finally both vectors were sorted at the same positions.
John Mary Abel Betty Tom
1 2 3 4 5
When swapping John at 0 and Abel at 2, you have to swap the 1 at 0 and the 3 at 2, so taht you temporarily have
Abel Mary John Betty Tom
3 2 1 4 5
i. e. John is always associated to 1 and Abel always has the 3.
Here was my resolved solution. Worked it out in school. Uses the Bubble sort Thank you to everyone!
for (unsigned int i=0; i<Vcar.size()-1; ++i) // Sort Vector
{
for (unsigned int j=1; j<Vcar.size(); ++j)
{
if(Vcar[j-1]>Vcar[j])
{
temp=Vcar[j];
Vcar[j] = Vcar[j-1];
Vcar[j-1] = temp;
temp1=Vmpg[j]; //keeps corresponding/associated vector with
Vmpg[j] = Vmpg[j-1]; //sorted vector
Vmpg[j-1] = temp1;
}
}
}
Business Accounts
Answer for Membership
by: evilrixPosted on 2009-11-04 at 05:54:10ID: 25739280
Use std::sort
eference/a lgorithm/s ort/
http://www.cplusplus.com/r
sort (v.begin(), v.end());