如何对 std::pair 的 std::vector 进行排序
按 pair 的 first 元素原地排序 std::vector<std::pair<...>>:
sort_by_first.cpp
std::sort(ret.begin(), ret.end(), [](const auto& a, const auto& b) {
return a.first < b.first;
});按 pair 的 second 元素原地排序 std::vector<std::pair<...>>:
sort_by_second.cpp
std::sort(ret.begin(), ret.end(), [](const auto& a, const auto& b) {
return a.second < b.second;
});Check out similar posts by category:
C/C++
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow