如何根据一对中的第一个和第二个元素对向量对进行排序?
How do I sort a vector of pairs based on both first and second element in a pair?
如果我有一个向量对
vector<pair<int, int>> arr;
并传递
这样的元素
4 5
3 7
10 5
5 7
1 5
我怎样才能让这个对对元素进行排序取决于像这样的对中的第一个和第二个元素按降序排列
5 7
3 7
10 5
4 5
1 5
或升序排列
1 5
4 5
10 5
3 7
5 7
编辑:我想要对矢量进行排序取决于两侧,例如
第二个元素 5 用第一个元素 (4, 10, 1) 和第二个元素 7 重复 3 次 用第一个元素 (3, 5) 重复 2 次
所以如果我按降序对它们进行排序,重复的 7 将排在第一位,然后 5就会这样
3 7
5 7
4 5
10 5
1 5
然后用 7 对第一个元素也按降序排序成为 (5, 3) 然后第一个元素用 5 变成 (10, 4, 1)
所以最终的数组将是这样的
5 7
3 7
10 5
4 5
1 5
您可以简单地使用 std::sort 对其进行升序排序
std::vector<std::pair<int, int>> arr;
arr.push_back({ 4, 5 });
arr.push_back({ 3, 7 });
arr.push_back({ 10, 5 });
arr.push_back({ 5, 7 });
arr.push_back({ 1, 5 });
std::sort(arr.begin(), arr.end());
此外,您可以使用 std::greater<>()
降序排序
std::sort(arr.begin(), arr.end(), std::greater<>());
编辑后:如果您的问题是按第二个元素排序,而不是如果第二个元素等于按第一个元素排序,那么这可能是一个解决方案
(按升序排列)
bool sortBySecondElement(const std::pair<int, int>& p1, const std::pair<int, int> & p2)
{
return p1.second < p2.second;
}
bool sortByFirstElement(const std::pair<int, int>& p1, const std::pair<int, int>& p2)
{
return p1.second == p2.second && p1.first < p2.first;
}
int main()
{
std::vector<std::pair<int, int>> arr;
arr.push_back({ 4, 5 });
arr.push_back({ 3, 7 });
arr.push_back({ 10, 5 });
arr.push_back({ 5, 7 });
arr.push_back({ 1, 5 });
std::sort(arr.begin(), arr.end(), sortBySecondElement);
std::sort(arr.begin(), arr.end(), sortByFirstElement);
}
使用 std::sort,并应用正确的排序标准:
#include <algorithm>
#include <vector>
#include <utility>
#include <iostream>
bool SortAscending(const std::pair<int,int>& p1, const std::pair<int,int>& p2)
{
return std::tie(p1.second, p1.first) < std::tie(p2.second, p2.first);
}
bool SortDescending(const std::pair<int,int>& p1, const std::pair<int,int>& p2)
{
return std::tie(p1.second, p1.first) > std::tie(p2.second, p2.first);
}
int main()
{
std::vector<std::pair<int, int>> arr =
{{4, 5},{3, 7}, {10, 5}, {5, 7},{1, 5}};
std::cout << "Original: \n";
for (auto& p : arr)
std::cout << p.first << " " << p.second << "\n";
std::sort(arr.begin(), arr.end(), SortAscending);
std::cout << "\n\nAscending: \n";
for (auto& p : arr)
std::cout << p.first << " " << p.second << "\n";
std::sort(arr.begin(), arr.end(), SortDescending);
std::cout << "\n\nDescending: \n";
for (auto& p : arr)
std::cout << p.first << " " << p.second << "\n";
}
输出:
Original:
4 5
3 7
10 5
5 7
1 5
Ascending:
1 5
4 5
10 5
3 7
5 7
Descending:
5 7
3 7
10 5
4 5
1 5
如果我有一个向量对
vector<pair<int, int>> arr;
并传递
4 5
3 7
10 5
5 7
1 5
我怎样才能让这个对对元素进行排序取决于像这样的对中的第一个和第二个元素按降序排列
5 7
3 7
10 5
4 5
1 5
或升序排列
1 5
4 5
10 5
3 7
5 7
编辑:我想要对矢量进行排序取决于两侧,例如 第二个元素 5 用第一个元素 (4, 10, 1) 和第二个元素 7 重复 3 次 用第一个元素 (3, 5) 重复 2 次
所以如果我按降序对它们进行排序,重复的 7 将排在第一位,然后 5就会这样
3 7
5 7
4 5
10 5
1 5
然后用 7 对第一个元素也按降序排序成为 (5, 3) 然后第一个元素用 5 变成 (10, 4, 1) 所以最终的数组将是这样的
5 7
3 7
10 5
4 5
1 5
您可以简单地使用 std::sort 对其进行升序排序
std::vector<std::pair<int, int>> arr;
arr.push_back({ 4, 5 });
arr.push_back({ 3, 7 });
arr.push_back({ 10, 5 });
arr.push_back({ 5, 7 });
arr.push_back({ 1, 5 });
std::sort(arr.begin(), arr.end());
此外,您可以使用 std::greater<>()
降序排序std::sort(arr.begin(), arr.end(), std::greater<>());
编辑后:如果您的问题是按第二个元素排序,而不是如果第二个元素等于按第一个元素排序,那么这可能是一个解决方案
(按升序排列)
bool sortBySecondElement(const std::pair<int, int>& p1, const std::pair<int, int> & p2)
{
return p1.second < p2.second;
}
bool sortByFirstElement(const std::pair<int, int>& p1, const std::pair<int, int>& p2)
{
return p1.second == p2.second && p1.first < p2.first;
}
int main()
{
std::vector<std::pair<int, int>> arr;
arr.push_back({ 4, 5 });
arr.push_back({ 3, 7 });
arr.push_back({ 10, 5 });
arr.push_back({ 5, 7 });
arr.push_back({ 1, 5 });
std::sort(arr.begin(), arr.end(), sortBySecondElement);
std::sort(arr.begin(), arr.end(), sortByFirstElement);
}
使用 std::sort,并应用正确的排序标准:
#include <algorithm>
#include <vector>
#include <utility>
#include <iostream>
bool SortAscending(const std::pair<int,int>& p1, const std::pair<int,int>& p2)
{
return std::tie(p1.second, p1.first) < std::tie(p2.second, p2.first);
}
bool SortDescending(const std::pair<int,int>& p1, const std::pair<int,int>& p2)
{
return std::tie(p1.second, p1.first) > std::tie(p2.second, p2.first);
}
int main()
{
std::vector<std::pair<int, int>> arr =
{{4, 5},{3, 7}, {10, 5}, {5, 7},{1, 5}};
std::cout << "Original: \n";
for (auto& p : arr)
std::cout << p.first << " " << p.second << "\n";
std::sort(arr.begin(), arr.end(), SortAscending);
std::cout << "\n\nAscending: \n";
for (auto& p : arr)
std::cout << p.first << " " << p.second << "\n";
std::sort(arr.begin(), arr.end(), SortDescending);
std::cout << "\n\nDescending: \n";
for (auto& p : arr)
std::cout << p.first << " " << p.second << "\n";
}
输出:
Original:
4 5
3 7
10 5
5 7
1 5
Ascending:
1 5
4 5
10 5
3 7
5 7
Descending:
5 7
3 7
10 5
4 5
1 5