vector and sort usage of STL template
@ (Algorithmic Learning) Topic description
Title: Enter arbitrary (user, score) sequence, you can get the results from high to low or from low to high arrangement, the same results. The rules are processed before the first entry arrangement .
Example:
Jack 70.
Peter 96
Tom 70
Smith 67
From high to low grades
Peter 96
Jack 70.
Tom 70
Smith 67
From low to high grades
Smith 67
Tom 70
Jack 70.
Peter , enter a description:
Enter multiple lines, first enter the number of people you want to sort, and then enter Sort method 0 (descending) or 1 (ascending) and enter their names and grades, separated by a space, with the output description:
Output the name and result in a specified manner, separated by a space between the name and the result.
Enter an example:
3
0
Fang 90
Yang 50
Ning 70
Output Example:
Fang 90
Ning 70
Yang 50
Reference Solution:
#include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std
;
typedef struct Node {string name;
int grade;
int seq;
} student; int bit = 0;
0: Descending output; 1: Ascending output bool Compare (Student A, student B) {if (bit = = 1) {if (A.grade = = B.grade) {
return A.seq < B.seq;
else {return A.grade < B.grade;
{else//bit = 0 o'clock {if (A.grade = B.grade) {return a.seq < B.seq;
else {return a.grade > B.grade;
int main () {int n;
while (CIN >> n >> bit) {vector<student> students;
for (int i = 0; i < n; i++) {student Stu;
Stu.seq = i;
CIN >> Stu.name >> Stu.grade;
Students.push_back (Stu);
//ORDER BY score--Ascending Sort (Students.begin (), students.end (), compare); for (Auto it = Students.begin (); it!= students.end (); it++) {cout << (*it). Name << "" &l
t;< (*it). grade << Endl;
} return 0; }
Analysis: Based on C + + STL template, the most critical sorting algorithm does not have to write their own. What fast platoon, heap platoon and so on as the foundation grasps is good, here mainly emphasizes directly uses the STL the sort function to help us to arrange the order directly.
For a generic vector<int>ins array, if just the number of stored numeric elements, direct sort (ins.begin (), Ins.end ()), the default is ascending sorting.
If the vector is in a custom structure, you can also use sort, but the collation needs to be defined by itself. Use a custom comparison function name only after the sort function, noting that it is not a function call, but a function name. That is, the function as a parameter, is called the outer function.
The specific idea of this problem is to define a structure to store students ' names and grades. The definition of an ordinal seq is to track the order of input and also as an argument for sorting.
So, the whole logic is very clear, is to handle the input data, call sort, output can be.
Verbose, available in C:
while (scanf ("%d", &a)!=eof)
{
...
}
For multiple case processing.
In C + + syntax, the above can also be used, but if you are processing with a CIN stream:
while (Cin >> a)
{
...
}
Indicates that the input stream is not empty. Very small and very basic details.
About the comparison function, which is a Boolean, and returns True if the return is false, indicating increment.
That is the left item minus the right item.