我有两个大小相同的 vector
vector<float> predictions; //say {1.22, 3.22, 2.22, 4.22}
vector<int> indices; //say {0, 1, 2, 3}
我使用降序对预测中的值进行排序
std::sort(predictions.rbegin(), predictions.rend()); //gives {4.22, 3.22, 2.22, 1.22}
现在我想在预测的同时对索引进行排序。
//to get {3, 1, 2, 0}
如何在不使用提升和自定义模板的情况下做到这一点?
最佳答案
您可以将这两个 vector 合并为一个类型,如 std::vector<std::pair<int, float>>
并对其进行排序。比较函数可以是这样的:
bool compareFunc(std::pair<int, float> &a, std::pair<int, float> &b)
{
return a.second > b.second;
}
然后像这样对合并后的数据进行排序:
std::sort(data.begin(), data.end(), compareFunc);
在此之后,您可以获得排序的部分,即它的第一个组件。
关于c++ - 如何在不使用 boost 或创建模板的情况下在 C++ 中同时对两个 vector 进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34878329/