gpt4 book ai didi

c++ - 我可以使用 std::partial_sort 对 std::map 进行排序吗?

转载 作者:搜寻专家 更新时间:2023-10-31 00:09:03 25 4
gpt4 key购买 nike

有两个数组,一个用于ids,一个用于scores,我想将这两个数组存储到一个std::map中,并使用std::partial_sort 找到五个最高分,然后打印他们的 id那么,有没有可能在 std::map 上使用 std::partial_sort

最佳答案

std::map 中,排序仅适用于键。您可以使用 vector 来做到这一点:

//For getting Highest first
bool comp(const pair<int, int> &a, const pair<int, int> &b){
return a.second > b.second;
}
int main() {
typedef map<int, int> Map;
Map m = {{21, 55}, {11, 44}, {33, 11}, {10, 5}, {12, 5}, {7, 8}};
vector<pair<int, int>> v{m.begin(), m.end()};
std::partial_sort(v.begin(), v.begin()+NumOfHighestScorers, v.end(), comp);
//....
}

这是 Demo

关于c++ - 我可以使用 std::partial_sort 对 std::map 进行排序吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45181110/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com