1, the results sorted - cattle-off

Title Description

Find and sort

Title: Enter any (user, results) sequence can be obtained from high to low or from low grade to high order, the same results
were treated according to the preceding entry arrangement rule.

Example:
Jack 70
peter 96
Tom 70
smith 67

High to low scores 
peter 96 
Jack 70 
Tom 70 
smith 67

From low to high

smith     67

jack      70 

Tom      70 
peter     96

 

Enter a description:

Enter multi-line, first enter the number you want to sort of person, then enter the sorting method 0 (in descending order) or 1 (in ascending order) were re-enter their names and achievements, separated by a space

Output Description:

Separated by a space between the specified manner in accordance with the output of the names and scores, names and scores

Example:

Input:

3
0
fang 90
yang 50
ning 70

Output:

90 Fang
Ning 70
Yang 50

Solutions


1, which is more than one condition scheduling problem. First we need to create a structure to hold the string and integer variables

2, using stable_sort () according to ascending, descending order.

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <string>
 4 #include <vector>
 5 using namespace std;
 6 
 7 typedef struct stu
 8 {
 9     string name;
10     int grade;
11 }student;
12 
13 int cmp1(student a,student b)
14 {
15     return a.grade > b.grade;
16 }
17 
18 int cmp2(student a,student b)
19 {
20     return a.grade < b.grade;
21 }
22 
23 
24 int main()
25 {
26     int count=0,cmp=0;
27     student s;
28     int grade;
29     string name;
30     int n = count;
31     while(cin >> count >> cmp)
32     {
33         vector<student> stuVect;
34         while(count--)
35         {
36             cin >> name >> grade;
37             s.name = name;
38             s.grade =grade;
39             stuVect.push_back(s);
40         }
41             if(cmp == 0)
42                 stable_sort(stuVect.begin(),stuVect.end(),cmp1);
43             if(cmp == 1)
44                 stable_sort(stuVect.begin(),stuVect.end(),cmp2); 
45         
46         for(auto it = stuVect.begin(); it != stuVect.end(); it++)
47         cout << it -> name << ' ' << it -> grade << endl;
48     }
49     return 0;
50 }

summary

Encountered a lot of trouble, and I realize that a lot of new knowledge through question head

1, sort () function is stable_sort () function

  • Need header <algorithm>
  • Syntax Description: sort (begin, end, cmp) stable_sort (begin, end, cmp), cmp argument can not, if there is no default non-descending order.
  • stable_sort () function ensures equal element relative to the original order in the sorted unchanged
  • This function can pass two parameters or three parameters. The first parameter is the first address range to be sorted, the second argument is the address of the next section end address. That is, the sort interval is [a, b);
  • Briefly, there is an array int a [100], To from a [0] to a [99] sort the elements, writing sort (a, a + 100) on the line, default sort non-descending order .
  • For vector variables is, sort (v.begin (), v.end ())

Here take note cmp function parameters:

If it is not less than the defined data type of operation, or to change the sort order, it is necessary to use a third parameter - the comparison function.

The comparison function is a function of their own definition, the return value is bool or integer, it specifies what kind of relationship is "less than." Just want an array of integers in descending order, you can define a comparison function cmp

1 bool cmp(int a,int b)
2 {
3    return a>b;
4 }

2, the container application vector

  • In the last push_back to add a data array
  • pop_back remove the last data array
  • Number of position data obtained at
  • head pointer array begin to give
  • end to obtain a pointer to the last cell array of +1

3, access vector 

1      // iterator will output data container 
2      vector < int > :: Iterator IT; // declare an iterator to access the vector container, effect: traversing pointing vector or the container element 
. 3      for (IT = obj.begin ();! obj.end IT = (); IT ++ )
 . 4      {
 . 5          COUT * IT << << "  " ;
 . 6      }

 

 

  

 

Guess you like

Origin www.cnblogs.com/jiashun/p/newcode.html