What is Vector of Pairs?
A pair is a container which stores two values mapped to each other, and a vector containing multiple number of such pairs is called a vector of pairs.
CPP
#include<bits/stdc++.h>
using namespace std;
int main()
{
vector< pair < int , int > > vect;
int arr[] = {10, 20, 5, 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]) );
for ( int i=0; i<n; i++)
{
cout << vect[i].first << " "
<< vect[i].second << endl;
}
return 0;
}
|
Output:
10 30
20 60
5 20
40 50
Time Complexity: O(n), where N is the size of the Vector.
Auxiliary Space: O(1)
Case 1 : Sorting the vector elements on the basis of first element of pairs in ascending order.
This type of sorting can be achieved using simple “ sort() ” function. By default the sort function sorts the vector elements on basis of first element of pairs.
CPP
#include<bits/stdc++.h>
using namespace std;
int main()
{
vector< pair < int , int > > vect;
int arr[] = {10, 20, 5, 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());
cout << "The vector after 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 applying sort operation is:
10 30
20 60
5 20
40 50
The vector after applying sort operation is:
5 20
10 30
20 60
40 50
Time Complexity: O(N*logN), where N is the size of the sorted vector.
Auxiliary Space: O(1)
Case 2 : Sorting the vector elements on the basis of second element of pairs in ascending order.
There are instances when we require to sort the elements of vector on the basis of second elements of pair. For that, we modify the sort() function and we pass a third argument, a call to an user defined explicit function in the sort() function.
CPP
#include<bits/stdc++.h>
using namespace std;
bool sortbysec( const pair< int , int > &a,
const pair< int , int > &b)
{
return (a.second < b.second);
}
int main()
{
vector< pair < int , int > > vect;
int arr[] = {10, 20, 5, 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(), sortbysec);
cout << "The vector after 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 applying sort operation is:
10 30
20 60
5 20
40 50
The vector after applying sort operation is:
5 20
10 30
40 50
20 60
Time Complexity: O(N*logN), where N is the size of the sorted vector.
Auxiliary Space: O(1)
Sorting Vector of Pairs in C++ | Set 2 (Sort in descending order by first and second)
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 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 :
27 Jan, 2023
Like Article
Save Article