C++ Sort Vector of Pairs
C++ provides the facility to display the vector of pairs and sort the vector of pairs. There is a container in C++ that holds two values that are mapped to one another called a “pair” and a “vector of pairs” which is a vector that has many of these pairs in it. Sorting means to arrange the data in ascending or descending order according to the requirements. Here, we will learn how to sort the vector of pairs in C++ programming. We can sort the vector of pairs in both “ascending” and “descending” orders in C++. We can easily sort the vector of pairs by utilizing the “sort()” method. Let’s do some codes here to sort the vector of pairs.
Example 1:
Let’s begin the code by including the header file here which is “bits/stdc++.h”. After having this header file, we don’t need to include more header files as it contains all the necessary libraries. Then, we add the “std” namespace and call the “main()” function.
Now, we declare a “vector of pairs” named “my_vect” and put the “int” data type so the data that we enter in these pairs is the “integer” data type. Below this, we initialize two arrays with the names “my_arr1[]” and “my_arr2[]”. Here, we initialize the pairs’ first and second values with these array values. Then, we use the “for” loop to enter these values into the vector of pairs. Here, we use the “push_back()” function which aids in inserting the values at the end of the vector. Inside this function, we place the “make_pair” option which is utilized to construct the pair object of two values of “my_arr1” and “my_arr2”.
After this, we print the vector of pairs by utilizing the “for” loop again. Here, the “first” and “second” keywords are added to get the first and second values of the vector of pairs. This prints the vector of pairs without sorting here. Now, we utilize the “sort()” function to sort the vector of pairs. Here, the “begin()” and “end()” functions are employed as passed the starting and end of the vector of pairs to this “sort()” function.
After sorting, we print the vector of pairs again by utilizing the “cout” and place the first and second keywords with “my_vec[i]”. Now, the sorted vector of pairs is also printed here.
Code 1:
using namespace std;
int main()
{
vector< pair <int,int> > my_vect;
int my_arr1[] = {49, 20, 15, 56};
int my_arr2[] = {37, 20, 90, 55};
int num = sizeof(my_arr1)/sizeof(my_arr1[0]);
for (int i=0; i<num; i++)
my_vect.push_back( make_pair(my_arr1[i],my_arr2[i]) );
cout << "Before sorting vector of pairs: "<< endl;
for (int i=0; i<num; i++)
{
cout << my_vect[i].first << " "
<< my_vect[i].second << endl;
}
sort(my_vect.begin(), my_vect.end());
cout << "\nAfter sorting vector of pairs: "<< endl ;
for (int i=0; i<num; i++)
{
cout << my_vect[i].first << " "
<< my_vect[i].second << endl;
}
return 0;
}
Output:
The vector of pairs before sorting is displayed first. Then, we apply the sorting technique on this vector of pairs. Then, the sorted vector of pairs is also displayed here. We can see that the vector of pairs is sorted in ascending order based on the first element of the pair.
Example 2:
In this example, we now create a “vector of pairs” called “vector_1” and assign the “int” data type which means that the information that we enter in these pairs is of the “integer” data type. Two arrays with the names “first_arr[]” and “second_arr[]” are initialized in the following. Here, we initialize these arrays’ values for the first and second pairs’ values.
Next, we enter these values into the vector of pairs using the “for” loop. The “make_pair” option, which is used to generate the pair object of two values from the “first_arr[]” and “second_arr[]” arrays, is placed inside the “push_back()” method which helps to insert the items at the end of the vector.
Next, we use the “for” loop once more to output the vector of pairs. To obtain the first and second values of the vector of pairs, the “first” and “second” keywords are inserted. This outputs the pair vector without any sorting at this point. We now sort the “vector of pairs” by utilizing a “sort()” function in descending order. In this function, we place the “rbegin()” and “rend()” functions so it will reverse the sorting and start from the end of the values of the pair. It arranges them in descending order.
Following the sorting, we use “cout” to output the vector of pairs after utilizing the “for” loop once more, inserting the first and second keywords with “vector_1[i]” in “cout”. This is where the pairings’ sorted vector is also printed.
Code 2:
using namespace std;
int main()
{
vector<pair<int, int> > vector_1;
int first_arr[] = { 77, 29, 97, 15 };
int second_arr[] = { 35, 89, 64, 25 };
int s = sizeof(first_arr) / sizeof(first_arr[0]);
for (int i = 0; i < s; i++)
vector_1.push_back(make_pair(first_arr[i], second_arr[i]));
cout << "Before Sorting:" << endl;
for (int i = 0; i < s; i++) {
cout << vector_1[i].first << " " << vector_1[i].second
<< endl;
}
sort(vector_1.rbegin(), vector_1.rend());
cout << endl << "After sorting:" << endl;
for (int i = 0; i < s; i++) {
cout << vector_1[i].first << " " << vector_1[i].second
<< endl;
}
return 0;
}
Output:
The pairs’ pre-sort vector is shown here first, followed by the pairs’ sorted vector which is likewise shown here after the sorting process is applied to it. As we can see, the initial element of each pair determines how the vector of pairs is sorted in descending order.
Example 3:
Here, we create a function of “bool” type named “sortBySecElement” to sort the vector numbers. In this function, we place a condition, the “value1.second < value2.second”, which compares the second values of both vectors of pairs and returns the numbers.
Then, the “main()” is invoked in the following where we create the vector of pairs. The following initializes two arrays named “new_array1[]” and “new_aray2[]”. Here, we insert the values of pairs in these arrays. Next, we use the “for” loop to input these values into the vector of pairs. Inside the “push_back()” method, which aids in inserting the items at the end of the vector, is the “make_pair” option which is utilized to create the pair object of two values from the “new_array1[]” and “new_array2[]” arrays.
We then output the vector of pairs using another “for” loop. The “first” and “second” keywords are inserted to get the first and second values of the vector of pairs. No sorting is done at this stage, and the pair vector is outputted. We now use the “sort()” function to sort it. The beginning and end of the vector of pairs are supplied to the “sort()” function in this case via the use of the “begin()” and “end()” functions. We also place the “sortBySecElement” function which we previously created inside this “sort()” function where we set the pattern of sorting vector of pairs from the second element of the vector in ascending order.
Now, we utilize the “for” loop again. Then, the first and second keywords are inserted with “new_vec[i]” in the “cout” to produce the vector of pairs again after sorting. This is also where the sorted vector of the pairs in ascending order is now printed.
Code 3:
using namespace std;
bool sortBySecElement(const pair<int,int> &value1,
const pair<int,int> &value2)
{
return (value1.second < value2.second);
}
int main()
{
vector< pair <int, int> > new_vec
int new_arr1[] = {34, 29, 65, 48 };
int new_arr2[] = {67, 19, 54, 7};
int value = sizeof(new_arr1)/sizeof(new_arr1[0]);
for (int i=0; i<value; i++)
new_vec.push_back( make_pair(new_arr1[i],new_arr2[i]) );
cout << "Before Sorting:" << endl ;
for (int i=0; i<value; i++)
{
cout << new_vec[i].first << " "
<< new_vec[i].second << endl;
}
sort(new_vec.begin(), new_vec.end(), sortBySecElement);
cout << endl << "After sorting:" << endl ;
for (int i=0; i<value; i++)
{
cout << new_vec[i].first << " "
<< new_vec[i].second << endl;
}
return 0;
}
Output:
Here, the sorted vector of pairs is displayed, and the sorting is done according to the second values of the pairs. The second element of pairs are stored in ascending order and displayed here.
Conclusion
This guide is all about the “sort vector of pairs” in C++. We explored the “vector of pairs” without sorting as well as sorting the “vector of pairs” in ascending and descending order. We illustrated this with the examples in which we sort the “vector of pairs” according to the first and second numbers of these pairs in C++ programming. We learned that the “sort()” method aids in doing this sorting.
Source: linuxhint.com