1

Let's suppose I have the following three vectors:

std::vector<int> classIds;
std::vector<float> confidences;
std::vector<Rect> boxes;

The data is pushed into these vectors as follows:

for (iterate over candidates)
{
    // compute classIdPoint
    if (confidence > confidence_Threshold)
    {
        // compute left, top, width, and height
        classIds.push_back(classIdPoint.x);
        confidences.push_back((float)confidence);
        boxes.push_back(Rect(left, top, width, height));
    }
}

The order at which the data is pushed in ties these vectors together. For instance, the confidence value in confidences.at(0) corresponds to the class ID at classIds.at(0).

Now I want to sort the confidences vector in descending order, which is easy to do

sort(confidences.begin(), confidences.end(), greater<float>());

But now I need the same sorting to be applied to the other two vectors (classIds and boxes) in order to maintain the relationship between the vectors. What would be a good way of doing this?

csg
  • 8,096
  • 3
  • 14
  • 38
  • 3
    can you use one vector of structs instead? – 463035818_is_not_an_ai Feb 12 '20 at 16:29
  • 3
    See here: https://stackoverflow.com/questions/17074324/how-can-i-sort-two-vectors-in-the-same-way-with-criteria-that-uses-only-one-of/46506051 – DeducibleSteak Feb 12 '20 at 16:31
  • 2
    Does this answer your question? [How can I sort two vectors in the same way, with criteria that uses only one of the vectors?](https://stackoverflow.com/questions/17074324/how-can-i-sort-two-vectors-in-the-same-way-with-criteria-that-uses-only-one-of) – Kostas Feb 12 '20 at 16:34

0 Answers0