Ivan Ng Ivan Ng - 1 month ago 11
C++ Question

How do I sort 2 different vector simultaneously

Currently I have the codes below. My objectives is to sort Value in decending order. However, I want partner vector to be sorted according to the position of value too. I managed to sort the value but unable to pair up with the partnerVector values.

Meaning, the Output I want it to be as follows:

1) Value : 5 at partner (160, 170)

2) Value : 4 at partner (100, 110)

3) Value : 3 at partner (120, 130)

4) Value : 2 at partner (200, 220)

5) Value : 1 at partner (140, 150)

Help is appreciated, thanks

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

vector<float> valueVector{ 4, 3, 1, 5, 2 };

vector<float> *valuePtr = &valueVector;

vector<int> partnerVector{100, 110, 120, 130, 140, 150, 160, 170, 200, 220 };

vector<int> *partnerPtr = &partnerVector;


int main()
{


int i = 0;

sort(valuePtr->begin(), valuePtr->end(), [](int a, int b) { return a>b; });

if (valuePtr->size() > 4) //if vector size is more than 4, means more than 5 records found, loop and display up to 5 records only
{
for (i; i <= 4; i++)
{
cout << i + 1 << ") " << "Value : " << valuePtr->at(i) << " at partner (" << partnerPtr->at(i * 2) << ", " << partnerPtr->at(i + 1 + i) << ")" << endl;
}
}
}

Answer

You have to link both lists together if you want to use standard algorithms. I chose to create a vector of float,int, the second being the index of the vector.

Once sorted, loop through the "shuffled" indexes and use them on the second list

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

vector<float> valueVector{ 4, 3, 1, 5, 2 };

vector<float> *valuePtr = &valueVector;

vector<int> partnerVector{100, 110, 120, 130, 140, 150, 160, 170, 200, 220 };

vector<int> *partnerPtr = &partnerVector;

typedef pair<float,int> MyPair;

int main()
{
    vector<MyPair> the_pair;
    for (size_t x=0;x<valueVector.size();x++)
    {
        the_pair.push_back(make_pair(valueVector[x],x));
    }


    int i = 0;

    sort(the_pair.begin(), the_pair.end(), [](const MyPair &a, const MyPair &b) { return a.first>b.first; });

    if (the_pair.size() > 4) //if vector size is more than 4, means more than 5 records found, loop and display up to 5 records only
    {
        for (i; i <= 4; i++)
        {
            cout << i + 1 << ")     " << "Value : " << the_pair.at(i).first << " at partner (" << partnerVector[the_pair.at(i).second*2] << "," << partnerVector[the_pair.at(i).second*2+1] << ")" << endl;
        }
    }
}

result:

1)     Value : 5 at partner (160,170)
2)     Value : 4 at partner (100,110)
3)     Value : 3 at partner (120,130)
4)     Value : 2 at partner (200,220)
5)     Value : 1 at partner (140,150)

the "trickier" part is the sort line. It sorts the list of pair (value,index) according to the first value of the pair but sorts the vector or pairs so the link between original index and value is preserved.

sort(the_pair.begin(), the_pair.end(), [](const MyPair &a, const MyPair &b) { return a.first>b.first; });
Comments