Sorting of Vector of Tuple in C++ (Ascending Order)


We will discuss sorting the vector of Tuple in C++ in ascending order in this article. A list of elements are stored in a C++ data structure known as a tuple. The same or different data types may be contained in it, and we may access them in the same sequence in which they were initialised as inputs. A tuple's data is organised so that we can retrieve it in the same order.

Syntax

tuple<data type-1, data type-2, data type-3,….> name

In C++, this is how we can initialise a tuple. We may require few more tuple functions to sort the vector of tuples:

make_tuple()

This function is used to create tuples. We can store values using this function in a tuple according to the arguments passed in initialising the tuple.

Syntax

tuple<int, int, string> t;
t=make_tuple(5, 4, “hello”);

The function make_tuple() will store the values passed to it into the tuple initialised.

get<>()

This function is for getting a particular tuple value or accessing the values of tuple.

Syntax

tuple<int, int, int, int> t=make_tuple(1, 2, 3, 4);
cout<<get<2>(t);

Output

3

NOTE:Tuple also follows 0-indexing same as vector to access the elements stored in it.

Vectors are a form of C++ data structure that can also be used to store lists of identical data typed elements. They are comparable to dynamic arrays in that they allow for runtime size changes.

Vector of any data type can be initialised with the following syntax :

#include <vector>
vector<data type> name;

We can pass any data type such as int, string, etc to initialise the vector of a particular data type.

We will talk about how to arrange a vector of tuples so that it is sorted ascendingly. The vector of tuples can be sorted by sorting the vector in relation to the first element contained in tuples, the second element stored in tuples, or in some other way.

Before sorting the vector of tuples, let’s just learn how to initialise a vector of tuples in C++.

Example

//C++ code to show vector of tuples is created and how to print it
#include <bits/stdc++.h>

using namespace std;

int main()
{
   //initialise a vector of tuples by passing tuple through it
   //initialise a tuple to store 4 integers
   vector<tuple<int, int, int, int>> vec;
   
   //store tuples in vector using push_back() function
   //tuples are initialised using make_tuple() function
   vec.push_back(make_tuple(2,2,4,9));
   vec.push_back(make_tuple(5,3,1,8));
   vec.push_back(make_tuple(7,3,-3,10));
   vec.push_back(make_tuple(8,12,32,6));
   
   int s=vec.size(); //to get the size of the vector
   
   for(int i=0;i<s;i++){ //to print the elements stored in vector of tuples, vec
      cout<<get<0>(vec[i])<<" "<<get<1>(vec[i])<<" "
      <<get<2>(vec[i])<<" "<<get<3>(vec[i])<<endl;
   }

   return 0;
}

Output

2 2 4 9
5 3 1 8
7 3 -3 10
8 12 32 6

Approach-1 (with respect to first element)

We can sort a vector of tuples using an in-built library provided in C++ i.e. sort().

This function sorts the elements in data structure in ascending order by default.

The syntax of the function is:

vector<int> v={5,8,7,3,1};
sort(v.begin(),v.end());

When using the sort() function, two parameters are typically given. The first parameter specifies the position from which we must order the components, and the second specifies the position up to which we must order the elements.Our criterion allows us to pass the third component as well. As an illustration, let's say we wish to sort a vector or array in decreasing order.

The sort() function by default sorts according to the first element in ascending order. The sort() function can be used to simply order the vector of tuples according to the first element that is contained in the tuple.

NOTE : If the first elements of the tuple are equal while sorting the vector of tuples with respect to the first element, then it will sort those tuples on the basis of their subsequent element by default.

The C++ code for the above approach:

Example

//function to sort vector of tuples with respect to first elements
#include <bits/stdc++.h>

using namespace std;

int main()
{
   //initialise a vector of tuples by passing tuple through it
   //initialise a tuple to store 4 integers
   vector<tuple<int, int, int, int>> vec;
   
   //store tuples in vector using push_back() function
   //tuples are initialised using make_tuple() function
   vec.push_back(make_tuple(10,2,14,9));
   vec.push_back(make_tuple(7,3,-3,10));
   vec.push_back(make_tuple(5,2,11,8));
   vec.push_back(make_tuple(8,12,5,6));
   
   //sort the vector of tuples in ascending order with respect to the first element
   sort(vec.begin(),vec.end());
   
   int s=vec.size(); //to get the size of the vector
   
   for(int i=0;i<s;i++){ //to print the elements stored in vector of tuples, vec
      cout<<get<0>(vec[i])<<" "<<get<1>(vec[i])<<" "
       <<get<2>(vec[i])<<" "<<get<3>(vec[i])<<endl;
   }

   return 0;
}

Output

5 2 11 8
7 3 -3 10
8 12 5 6
10 2 14 9

Time Complexity : O(N logN) where N is the size of the vector.

Space Complexity : O(1) as we have not used any extra space.

Approach-2 (with respect to third element)

In this case, we will be sorting the vector of tuples with respect to the third element present in the tuple. To sort the vector in reference with the third element in the tuple, we will just use the sort() function as in the above approach but will modify it.

As we all know, we can pass a third parameter too in the sort() function based on our requirement to sort the elements in the way we want.

In order to sort the vector on the basis of the third element, we will pass a boolean function as a third parameter just to check if the third element of the tuple is less than the third element of the next tuple to sort it in ascending order.

The C++ code to sort with respect to third element in tuple:

Example

//C++ code to sort the vector of tuples with respect to third element
#include <bits/stdc++.h>

using namespace std;

//function to compare third elements present in the tuple
bool third(tuple<int, int, int, int>& p,
         tuple<int, int, int, int>& q){
            return get<2>(p)<get<2>(q);
         }

int main()
{
   //initialise a vector of tuples by passing tuple through it
   //initialise a tuple to store 4 integers
   vector<tuple<int, int, int, int>> vec;
   
   //store tuples in vector using push_back() function
   //tuples are initialised using make_tuple() function
   vec.push_back(make_tuple(10,2,14,9));
   vec.push_back(make_tuple(7,3,-3,10));
   vec.push_back(make_tuple(5,2,11,8));
   vec.push_back(make_tuple(8,12,5,6));
   
   //sort the vector of tuples in ascending order with respect to the first element
   sort(vec.begin(),vec.end(),third);
   
   int s=vec.size(); //to get the size of the vector
   
   for(int i=0;i<s;i++){ //to print the elements stored in vector of tuples, vec
      cout<<get<0>(vec[i])<<" "<<get<1>(vec[i])<<" "
       <<get<2>(vec[i])<<" "<<get<3>(vec[i])<<endl;
   }

   return 0;
}

Output

7 3 -3 10
8 12 5 6
5 2 11 8
10 2 14 9

Time Complexity : O(N logN) where N is the size of the vector.

Space Complexity : O(1) since we have not used any extra space.

Similarly we can sort the vector of tuples on the basis of any element present in the tuple by comparing the specified element in the tuple in a boolean function.

Conclusion

We have discussed sorting any vector of tuples in ascending order in C++ with respect to any element present in the tuple. We discussed how to use the sort() function to sort the vector of tuples and sorting the vector of tuples with respect to any position of elements in the tuple by passing the third parameter in the sort() function.

I hope after reading the article, all your doubts regarding the topic are being clarified.

Updated on: 21-Aug-2023

672 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements