Sorting of Vector of Tuple in C++ (Descending Order)
What is Vector of Tuple?
A tuple is an object that can hold a number of elements and a vector containing multiple number of such tuple is called a vector of tuple. The elements can be of different data types. The elements of tuples are initialized as arguments in order in which they will be accessed.
#include <bits/stdc++.h>
using namespace std;
int main()
{
vector<tuple< int , int , int > > v;
v.push_back(make_tuple(10, 20, 30));
v.push_back(make_tuple(15, 5, 25));
v.push_back(make_tuple(3, 2, 1));
for ( int i = 0; i < v.size(); i++)
cout << get<0>(v[i]) << " "
<< get<1>(v[i]) << " "
<< get<2>(v[i]) << "\n" ;
return 0;
}
|
Output:
10 20 30
15 5 25
3 2 1
Different ways to sort vector of tuples
Case 1 : Sorting the vector elements on the basis of first element of tuples in Descending order.
For these instances, we modify the sort() function and we pass a third argument, a call to an user defined explicit function in the sort() function.
#include <bits/stdc++.h>
using namespace std;
bool sortdesc( const tuple< int , int , int >& a,
const tuple< int , int , int >& b)
{
return (get<0>(a) > get<0>(b));
}
int main()
{
vector<tuple< int , int , int > > v;
v.push_back(make_tuple(10, 20, 30));
v.push_back(make_tuple(15, 5, 25));
v.push_back(make_tuple(3, 2, 1));
sort(v.begin(), v.end(), sortdesc);
cout << "Sorted Vector of Tuple on basis"
" of first element of tuple:\n" ;
for ( int i = 0; i < v.size(); i++)
cout << get<0>(v[i]) << " "
<< get<1>(v[i]) << " "
<< get<2>(v[i]) << "\n" ;
return 0;
}
|
Output:
Sorted Vector of Tuple on basis of first element of tuple:
15 5 25
10 20 30
3 2 1
Case 2 : Sorting the vector elements on the basis of second element of tuples in descending order.
There are instances when we require to sort the elements of the vector on the basis of second elements of tuples. For that, we modify the sort() function and we pass the third argument, a call to a user-defined explicit function in the sort() function.
#include <bits/stdc++.h>
using namespace std;
bool sortbysec( const tuple< int , int , int >& a,
const tuple< int , int , int >& b)
{
return (get<1>(a) > get<1>(b));
}
int main()
{
vector<tuple< int , int , int > > v;
v.push_back(make_tuple(10, 20, 30));
v.push_back(make_tuple(15, 5, 25));
v.push_back(make_tuple(3, 2, 1));
sort(v.begin(), v.end(), sortbysec);
cout << "Sorted Vector of Tuple on basis"
" of Second element of tuple:\n" ;
for ( int i = 0; i < v.size(); i++)
cout << get<0>(v[i]) << " "
<< get<1>(v[i]) << " "
<< get<2>(v[i]) << "\n" ;
return 0;
}
|
Output:
Sorted Vector of Tuple on basis of Second element of tuple:
10 20 30
15 5 25
3 2 1
Case 3 : Sorting the vector elements on the basis of third element of tuples in descending order.
There are instances when we require to sort the elements of the vector on the basis of third elements of tuples. For that, we modify the sort() function and we pass the third argument, a call to a user-defined explicit function in the sort() function.
#include <bits/stdc++.h>
using namespace std;
bool sortbyth( const tuple< int , int , int >& a,
const tuple< int , int , int >& b)
{
return (get<2>(a) > get<2>(b));
}
int main()
{
vector<tuple< int , int , int > > v;
v.push_back(make_tuple(10, 20, 30));
v.push_back(make_tuple(15, 5, 25));
v.push_back(make_tuple(3, 2, 1));
sort(v.begin(), v.end(), sortbyth);
cout << "Sorted Vector of Tuple on basis"
" of Third element of tuple:\n" ;
for ( int i = 0; i < v.size(); i++)
cout << get<0>(v[i]) << " "
<< get<1>(v[i]) << " "
<< get<2>(v[i]) << "\n" ;
return 0;
}
|
Output:
Sorted Vector of Tuple on basis of Third element of tuple:
10 20 30
15 5 25
3 2 1
Last Updated :
21 Aug, 2018
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...