We have discussed some of the cases of sorting vector of pairs in below set 1.
Sorting Vector of Pairs in C++ | Set 1 (Sort by first and second)
More cases are discussed in this article. Sometimes we require to sort the vector in reverse order. In those instances, rather than first sorting the vector and later using “reverse” function increases the time complexity of the code. Therefore, to avoid this we sort the vector in descending order directly.
Case 3 : Sorting the vector elements on the basis of first element of pairs in descending order.
This type of sorting arranges selected rows of pairs in vector in descending order. This is achieved by using “sort()” and passing iterators of 1D vector as its arguments.
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
vector<pair< int , int > > vect;
int arr[] = { 5, 20, 10, 40 };
int arr1[] = { 30, 60, 20, 50 };
int n = sizeof (arr) / sizeof (arr[0]);
for ( int i = 0; i < n; i++)
vect.push_back(make_pair(arr[i], arr1[i]));
cout << "The vector before applying sort is:\n" ;
for ( int i = 0; i < n; i++) {
cout << vect[i].first << " " << vect[i].second
<< endl;
}
sort(vect.rbegin(), vect.rend());
cout << "The vector after applying sort is:\n" ;
for ( int i = 0; i < n; i++) {
cout << vect[i].first << " " << vect[i].second
<< endl;
}
return 0;
}
|
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
vector<pair< int , int > > vect;
int arr[] = { 5, 20, 10, 40 };
int arr1[] = { 30, 60, 20, 50 };
int n = sizeof (arr) / sizeof (arr[0]);
for ( int i = 0; i < n; i++)
vect.push_back({arr[i], arr1[i]});
cout << "The vector before applying sort is:\n" ;
for ( int i = 0; i < n; i++) {
cout << vect[i].first << " " << vect[i].second
<< endl;
}
sort(vect.begin(), vect.end(),
greater<pair< int , int > >());
cout << "The vector after applying sort is:\n" ;
for ( int i = 0; i < n; i++) {
cout << vect[i].first << " " << vect[i].second
<< endl;
}
return 0;
}
|
Output
The vector before applying sort is:
5 30
20 60
10 20
40 50
The vector after applying sort is:
40 50
20 60
10 20
5 30
Time Complexity: O(N*logN), where N is the size of the vector.
Auxiliary Space: O(N)
Case 4 : Sorting the vector elements on the basis of second element of pairs in descending order.
These instances can also be handled by modifying “sort()” function and again passing a call to user-defined function.
CPP
#include<bits/stdc++.h>
using namespace std;
bool sortbysecdesc( const pair< int , int > &a,
const pair< int , int > &b)
{
return a.second>b.second;
}
int main()
{
vector< pair < int , int > > vect;
int arr[] = {5, 20, 10, 40 };
int arr1[] = {30, 60, 20, 50};
int n = sizeof (arr)/ sizeof (arr[0]);
for ( int i=0; i<n; i++)
vect.push_back( make_pair(arr[i],arr1[i]) );
cout << "The vector before sort operation is:\n" ;
for ( int i=0; i<n; i++)
{
cout << vect[i].first << " "
<< vect[i].second << endl;
}
sort(vect.begin(), vect.end(), sortbysecdesc);
cout << "The vector after applying sort operation is:\n" ;
for ( int i=0; i<n; i++)
{
cout << vect[i].first << " "
<< vect[i].second << endl;
}
return 0;
}
|
Output
The vector before sort operation is:
5 30
20 60
10 20
40 50
The vector after applying sort operation is:
20 60
40 50
5 30
10 20
Time Complexity: O(N*logN), where N is the size of the vector.
Auxiliary Space: O(N)
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
06 Mar, 2023
Like Article
Save Article