Requirement: There is a structure array of student information, including class, score, age, and other information. A student's class is now required to be sorted based on the primary keyword. If the class is the same, it is sorted by the score, if the score is the same, the score is sorted by age.
Reference Code
# Include <iostream>
# Include <algorithm>
# Include <vector>
Using namespace STD;
Struct student
{
Int class;
Int rank;
Int age;
};
Vector <student> Stu;
Bool CMP (const student & A, const student & B)
{
If (A. Class! = B. Class)
{
Return A. Class <B. Class;
}
Else if (A. rank! = B. rank)
{
Return A. Rank <B. rank;
}
Else
{
Return A. age <B. Age;
}
}
Int main ()
{
Student TMP;
Int num = 5;
While (Num --)
{
Cin> TMP. Class> TMP. Rank> TMP. Age;
Stu. push_back (TMP );
}
Sort (STU. Begin (), Stu. End (), CMP );
For (INT I = 0; I <5; I ++)
{
Cout <STU [I]. class <"\ t" <STU [I]. rank <"\ t" <STU [I]. age <Endl;
}
System ("pause ");
Return 0;
}
Reference data:
1 4 1
1 3 3
2 3 3
2 3 1
4 2 3
Running result:
Requirements:
There is a string consisting of letters and numbers, which can be uppercase or lowercase. Now you need to place the numbers between lowercase letters and uppercase letters. The letters follow the lexicographically ascending order of numbers.
Reference data:
Ofahiugh5092foiiuioq2t02ogihaohgawi5209ghaoiuhg
Reference code:
# Include <iostream>
# Include <algorithm>
# Define size 100
# Define is_num (x) (x <= '9' & x> = '0 ')
# Define is_lowcase (x) (x> = 'A' & x <= 'Z ')
# Define is_upcase (x) (x> = 'A' & x <'Z ')
Using namespace STD;
Bool CMP (const char & A, const char & B)
{
If (is_num (a) & is_num (B ))
{
Return a <B;
}
Else if (is_lowcase (a) & is_lowcase (B ))
{
Return a <B;
}
Else if (is_upcase (a) & is_upcase (B ))
{
Return a <B;
}
Else if (is_num (a) & is_lowcase (B ))
{
Return true;
}
Else if (is_num (B) & is_lowcase ())
{
Return false;
}
Else if (is_num (a) & is_upcase (B ))
{
Return false;
}
Else if (is_num (B) & is_upcase ())
{
Return true;
}
Else if (is_lowcase (a) & is_upcase (B ))
{
Return false;
}
Else if (is_lowcase (B) & is_upcase ())
{
Return true;
}
}
Int main ()
{
Char A [size];
While (CIN>)
{
Int Len = strlen ();
Sort (A, A + Len, CMP );
Cout <"after sort:" <Endl;
Cout <A <Endl;
}
Return 0;
}
Running result:
Requirement: If you give a bunch of numbers with positive and negative values, you must take all the negative numbers to the front of the positive number. The relative positions of positive and positive numbers cannot be changed.
Reference data:
15
3 4-3 4-5-8 5 1 6-2 5-1 5-3-4
Reference code:
# Include <iostream>
# Include <algorithm>
# Define size 100
Using namespace STD;
Bool CMP (const Int & A, const Int & B)
{
If (A <0 & B> 0)
{
Return true;
}
Return false;
}
Int main ()
{
Int array [size];
Int length;
While (CIN> length)
{
For (INT I = 0; I <length; I ++)
{
Cin> array [I];
}
Sort (array, array + length, CMP );
Cout <"after sort:" <Endl;
For (INT I = 0; I <length; I ++)
{
Cout <array [I] <"";
}
}
Return 0;
}
Running result:
The CMP function can use greater <int> (); (sort in ascending order) less <int> (); (sort in ascending order ).