Loading
svg
Open

C++ Sort vector of pairs

January 21, 20243 min read

C++ provides facilities for displaying a vector of pairs and sorting a vector of pairs. In C++ there is a container containing two values ​​that are mapped to each other called a “pair” and a “vector of pairs” which is a vector containing many of these pairs. Sorting means arranging the data in ascending or descending order as per the requirements. Here, we will learn how to sort a vector of pairs in C++ programming. We can sort a vector of pairs in both “ascending” and “descending” order in C++. We can easily sort the vector of pairs using the “sort()” method. Let's create some code here to sort the vector of pairs.

Example 1:

Let's start the code here by including the header file which is “bits/stdc++.h”. After having this header file, we do not need to include any more header files as it includes all the necessary libraries. Then, we add the “std” namespace and call the “main()” function.

Now, we declare a “vector of pairs” called “my_vect” and put “int” data type so that the data we enter in these pairs will be “integer” data type. Below this, we initialize two arrays named “my_arr1″[]” and ” my_arr2[], Here, we initialize the first and second value of the pair with these array values. Then, we use a “for” loop to enter these values ​​into a vector of pairs. Here, we use the “push_back()” function which helps in inserting the value at the end of the vector. Inside this function, we put the option “make_pair” which is used to create pair object of two values ​​“my_arr1” and “my_arr2”.

Next, we print the vector of pairs using the “for” loop again. Here, “first” and “second” keywords are added to get the first and second values ​​of the pair vector. It prints the vector of pairs without sorting here. Now, we use the “sort()” function to sort the vector of pairs. Here, “begin()” and “end()” functions are employed as the start and end of the vector of pairs in this “sort()” function.

After sorting, we print the vector of pairs again using “cout” and keep the first and second keywords with “my_vec”.[i], Now, the sorted vector of pairs is also printed here.

Code 1:

#Involved<बिट्स/stdc++.h>

using namespace std;

int main,,

,

vector< couple <int, int, , my_vector;

int my_arr1[] , ,49, 20, 15, 56,,

int my_arr2[] , ,37, 20, 90, 55,,

int number = sizeof,my_arr1,,size of,my_arr1[0],,

For ,int here I,0, I<whether i++,

my_vect.push_back, Make Pair,my_arr1[i],my_arr2[i], ,,

court , “Before sorting a vector of pairs:”, Andal;

For ,int here I,0, I<whether i++,

,

court , my_vect[i].First , ,

, my_vect[i].Second , Andal;

,

sort,my_vect.begin,,my_vect.end,,,,

court , ,\nAfter sorting the vector of pairs: “, andl;

For ,int here I,0, I<whether i++,

,

court , my_vect[i].First , ,

, my_vect[i].Second , Andal;

,

return 0,

,

Output:

The vector of pairs is displayed first before sorting. Then, we apply the sorting technique on this vector of pairs. Then, the ordered 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 specify “int” data type which means that the information we enter in these pairs is of “integer” data type. . Two tables named “first_arr”[]” and ” second_arr[]”is initialized in the following. Here, we initialize the values ​​of these arrays to the first and second pair of values.

Next, we enter these values ​​into a vector of pairs using a “for” loop. “make_pair” option, which is used to generate pair object of two values ​​from “first_ar”[]” and ” second_arr[]” The arrays are placed inside the “push_back()” method which helps in inserting the items at the end of the vector.

Next, we use the “for” loop once again to output a vector of pairs. To get the first and second value of the vector of pairs, “first” and “second” keywords are inserted. It outputs the pair vector without any sorting at this point. Now we sort the “vector of pairs” in descending order using the “sort()” function. In this function, we place the “rbegin()” and “rend()” functions so that it reverses the sorting and starts from the end of the pair of values. It arranges them in descending order.

After sorting, we use “cout” to output a vector of pairs, inserting the first and second keywords with “vector_1” after using the “for” loop once again.[i]” in “cout”. This is also where the ordered vector of pairs is printed.

Code 2:

#includes

using namespace std;

int main,,

,

vector<couple<int, int, , vector_1;

int first_arr[] , , 77, 29, 97, 15 ,,

int seconds_arr[] , , 35, 89, 64, 25 ,,

int s = sizeof,first_arrest, , size of,first_arrest[0],,

For ,integer i = 0, I < S; i++,

vector_1.push_back,Make Pair,first_arrest[i]second_arr[i],,,

court , “Before sorting:” , Andal;

For ,integer i = 0, I < S; i++, ,

court , vector_1[i].First , , , vector_1[i].Second

, Andal;

,

sort,vector_1.rbegin,,vector_1.rand,,,,

court , finally , “After sorting:” , Andal;

For ,integer i = 0, I < S; i++, ,

court , vector_1[i].First , , , vector_1[i].Second

, Andal;

,

return 0,

,

Output:

The pre-sorted vector of pairs is shown here first, followed by the sorted vector of pairs, which is shown here after a similar sorting process has been applied. As we can see, the starting element of each pair determines how the vector of pairs is sorted in descending order.

Example 3:

Here, we create a function of type “bool” called “sortBySecElement” to sort the vector numbers. In this function, we put a condition, “value1.sec < value2.sec", which compares the second values ​​of both vectors of the pair and returns numbers.

Then, “main()” is invoked in the following where we create a vector of pairs. The following initializes two arrays named “new_array1″[]” and ” new_array2[], Here, we insert the values ​​of the pairs into these arrays. Next, we use a “for” loop to input these values ​​into a vector of pairs. Inside the “push_back()” method, which helps to insert the item at the end of the vector, there is the “make_pair” option which is used to create a pair object of two values ​​from “new_array1″.[]” and ” new_array2[]“Array.

We then output the vector of pairs using another “for” loop. The keywords “first” and “second” 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 output. Now we use the “sort()” function to sort it. The beginning and end of the vector of pairs are supplied to the “sort()” function through the use of the “begin()” and “end()” functions in this case. We also place the “sortBySecElement” function that we created earlier inside this “sort()” function, where we set the pattern of sorting the vector of pairs from the second element of the vector in ascending order.

Now, we use the “for” loop again. Then, the first and second keywords are inserted with “new_vec”[i]In “cout” to regenerate the vector of pairs after sorting. This is also where the sorted vector of pairs in ascending order is now printed.

Code 3:

#Involved<बिट्स/stdc++.h>

using namespace std;

bool sortBySecElement,constant pair<int, int, ,value1,

constant pair<int, int, ,value 2,

,

return ,value1.second < value2.seconds,,

,

int main,,

,

vector< couple <int, int, , new thing

int new_arr1[] , ,34, 29, 65, 48 ,,

int new_arr2[] , ,67, 19, 54, 7,,

integer value = sizeof,new_arr1,,size of,new_arr1[0],,

For ,int here I,0, I<price; i++,

new_vec.push_back, Make Pair,new_arr1[i],new_arr2[i], ,,

court , “Before sorting:” , andl;

For ,int here I,0, I<price; i++,

,

court , new thing[i].First , ,

, new thing[i].Second , Andal;

,

sort,new_vec.begin,,new_vec.end,,sortBySecElement,,

court , finally , “After sorting:” , andl;

For ,int here I,0, I<price; i++,

,

court , new thing[i].First , ,

, new thing[i].Second , Andal;

,

return 0,

,

Output:

Here, a sorted vector of pairs is displayed, and sorting is done according to the second value of the pairs. The second element of the pairs is stored in ascending order and displayed here.

conclusion

This guide is about “sorting vectors of pairs” in C++. We searched “vector of pairs” without sorting as well as sorted “vector of pairs” in ascending and descending order. We have illustrated this with examples in which we sort a “vector of pairs” according to the first and second numbers of these pairs in C++ programming. We learned that the “sort()” method helps perform this sorting.

How do you vote?

0 People voted this article. 0 Upvotes - 0 Downvotes.
svg

What do you think?

Show comments / Leave a comment

Leave a reply

Loading
svg