C ++ sorts a group of pair data (used by the sort function)
Recently, when writing an algorithm, some data is stored in pair, and the data needs to be sorted based on the value of first or second in pair. For example, input data (), () are sorted in ascending order based on the first value, and output values), (3, 3), (4, 2 ).
After thinking, the implementation method is as follows:
First, store the data in the vector Array. vector <pair <int, int> vec;
Then use the sort function to sort the array. The use of the sort function is involved here.
The following describes how to use the sort function.
Function declaration:
Template <class RandomAccessIterator>
Void stable_sort (RandomAccessIterator first, RandomAccessIterator last );
Template <class RandomAccessIterator, class Compare>
Void stable_sort (RandomAccessIterator first, RandomAccessIterator last,
Compare comp );
Parameter description:
(1) The first is the starting address of the array to be sorted.
(2) The second is the ending address (the next address of the last address to be sorted ).
(3) The third parameter is the sorting method. It can be from large to small, but from small to large. The third parameter can also be left blank. The default sorting method is from small to large.
We can customize the third cmp function as needed. For example, to sort the integer array in descending order, we can write the cmp function as follows:
Bool cmp (int a, int B)
{
Return a> B; // The Ascending Order is a <B
}
This cmp parameter is very convenient to use because it can solve our pair Sorting Problem.
We only need to define the parameters of the cmp function according to the element type in the array and define the function body as needed.
Bool cmp (pair <int, int> a, pair <int, int> B)
{
Return a. first <B. first; // sort by fisrt in ascending order.
// Return a. second <B. second; // sort by second value in ascending order
}
Then, call the sort function sort (vec. begin (), vec. end (), cmp ).
You can sort the values of first in pair in ascending order.
The following is the code implementation
# Include "stdafx. h"
# Include <iostream>
# Include <vector>
# Include <algorithm>
Using namespace std;
// Sort by first value in ascending order
Bool cmp1 (pair <int, int> a, pair <int, int> B)
{
Return a. first <B. first;
}
// Sort by second value in ascending order
Bool cmp2 (pair <int, int> a, pair <int, int> B)
{
Return a. second <B. second;
}
Int main ()
{
Vector <pair <int, int> vec;
Vec. push_back ({1, 2 });
Vec. push_back ({4, 2 });
Vec. push_back ({3, 3 });
Vec. push_back ({2, 1 });
Sort (vec. begin (), vec. end (), cmp1 );
Cout <"sort by first value in ascending order:" <endl;
For (auto it = vec. begin (); it! = Vec. end (); it ++)
{
Cout <"(" <it-> first <"," <it-> second <")" <endl;
}
Sort (vec. begin (), vec. end (), cmp2 );
Cout <"sort by second values in ascending order:" <endl;
For (auto it = vec. begin (); it! = Vec. end (); it ++)
{
Cout <"(" <it-> first <"," <it-> second <")" <endl;
}
}
Run
This article permanently updates link: https://www.bkjia.com/Linux/2018-03/151558.htm