if i have a vector of pairs vector<pair<int, int>> arr;
and passes elements like
4 5
3 7
10 5
5 7
1 5
how can i make the pair to sort the elements depend on the first and second element in a pair like this in descending order
5 7
3 7
10 5
4 5
1 5
or in ascending order
1 5
4 5
10 5
3 7
5 7
Edit: what i want sort the vector depend on both sides for example second element 5 repeated 3 times with first elements (4, 10, 1) and second element 7 repeated 2 times with first elements (3, 5)
so if i sort them in descending order the repeated 7 will come first then 5 it will be like that
3 7
5 7
4 5
10 5
1 5
then sort the first elements with 7 also in descending order to become (5, 3) then first elements with 5 to become (10, 4, 1) so the final array will be like that
5 7
3 7
10 5
4 5
1 5
Use std::sort , and apply the proper sort criteria:
#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";
}
Output:
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
You can simply use the std::sort to sort it in ascending order
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());
In addition you can sort in descending order using std::greater<>()
std::sort(arr.begin(), arr.end(), std::greater<>());
After Edit: If your question is to sort by second element than if the second element are equal to sort by the first element than this might a solution
(it in ascending order)
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);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.