Vector sorting in C ++
When sorting by vector, it is found that there is a problem with the sort () function. sort sorting only returns the sorted results and does not return the original numbers of the results of each sort.
// Template
Bool cmp (const pair
& Amp; x, const pair
& Y) {return x. second> y. second;} // template
Void sortVector (vector
& Value, vector
& Lable) {lable. clear (); // defines an array of key-value pairs. The first Int is the key, and the second Int Is the value vector.
> TheMap; for (vector
: Size_type I = 0; I
>:: Size_type I = 0; I
The above code can solve this problem. The Calling method of the above Code is as follows:
Vector
A; a. push_back (1); a. push_back (2); a. push_back (4); a. push_back (3); vector
Lable; sortVector (a, lable); cout <
The principle is actually very simple. It is to combine data and numbers into a key-value pair, then define these key-value pairs as a vector <> variable, and then sort the vector with the sort function.