0

I have a class that defined as

class chromosome
{
   vector<vector <int> > P(3,vector <int> (5,0));
   vector<int> F;       
}

I want to sort F in descending order, and the P corresponding with F will be sorted. For example, Before sort

    P    F
    101  4
    111  8
    001  2
    110  5
    100  3

After sort

    P    F
    111  8
    110  5
    101  4
    100  3
    001  2

How to implelement it by C++. I tried to used sort function in vector class. However, it only sort F. The P order is not change This is my code

std::sort(F.rbegin(),F.rend());
John
  • 2,838
  • 7
  • 36
  • 65

2 Answers2

0

The way to do this is by sorting them after associating the order of P with the order of F, i.e. the first element in P sticks with the first element in F and so on. To do this you can use the make_pair command.

vector< pair<int, vector<int> > > X;
for (int i = 0; i < F.size(); i++)
  X.push_back(make_pair(F[i], P[i]));

This will create a vector consisting of pairs of elements of F and P. After this, you need to sort the array. Calling the simple sort function is defined for pairs in the manner that the first elements are compared first, followed by the second one. So, to sort the vector X, the command would be

sort(X.begin(), X.end())

After this, you need to put back the values from the sorted vector X into F and P. To do this:

for (int i = 0; i < F.size(); i++)
{
  F[i] = X[i].first;
  P[i] = X[i].second;
}
therainmaker
  • 4,253
  • 1
  • 22
  • 41
  • That would be for `sort`ing a `vector`. He is talking about sorting the two internal `vector`s in synch with each other. – BoBTFish Apr 02 '15 at 09:07
  • Your edit doesn't make a lot of sense. Think about how your instructions could be turned into code solving the problem. (i.e. as far as I can make out, they can't.) – BoBTFish Apr 02 '15 at 09:11
  • @BobTFish: I have posted a comprehensive answer. Feedback would be welcome. – therainmaker Apr 02 '15 at 09:18
  • It had error sir. error C2064: term does not evaluate to a function taking 1 arguments – John Apr 02 '15 at 09:42
  • @user8264 That's because he tries to access `vector` elements using `()` instead of `[]`. – BoBTFish Apr 02 '15 at 09:46
  • Yes. I got it. But it still has error error C2664: 'void std::vector<_Ty,_Alloc>::push_back(int &&)' : cannot convert parameter 1 from 'std::pair<_Ty1,_Ty2>' to 'int &&' and error C2228: left of '.first' must have class/struct/union – John Apr 02 '15 at 09:49
  • @user8264: This error was due to a mistake on my part. In the code example given above, I had forgotten to mention `pair` while defining X. Let me know if there are any other issues. – therainmaker Apr 02 '15 at 10:15
  • Thank for your solution. I have more knowledge about vector class – John Apr 02 '15 at 10:28
  • @BoBTFish: Thanks for pointing out the mistake again. I have been working a lot with MATLAB recently, and so my syntax is messed up. – therainmaker Apr 02 '15 at 10:38
0

It's not trivial to sort by key using STL. Sorting with std::sort require single iterator for begin and end, so you have to write some wrapper that will handle both vectors as single iterator.

In general:

  • moving iterator (operator++ and operator--) should move 2 underlying iterators,
  • comparison should compare only by the keys
  • you have to handle element swapping in 2 underlying iterators

Take a look at Sorting two arrays simultaneously. It's detailed description how to solve your problem.

Alternatively, maybe you can consider using map< int, vector<int> > instead of two vectors?

tmp
  • 1,079
  • 9
  • 16
  • [Please don't just post link-only answers.](http://meta.stackexchange.com/a/8259/242291) In its current form, your answer should just be a comment. Please add some explanation of the technique to your answer. (The link could still be a useful addition to your answer though). – BoBTFish Apr 02 '15 at 09:15