STL (Standard Temlate Libray) standard template library (1) sort usage
内容:sort的多种用法
内容还算全面,如有不足日后学习中进行补充
STL overview
Content: Contains some commonly used algorithms such as sorting and searching, as well as commonly used data types such as variable length arrays, linked lists, dictionaries, etc.
Features: easy to use, high efficiency
Header file: #include (English: algorithm Chinese translation: algorithm)
1. Sorting algorithm sort
Usage one (integer array is sorted from small to large)
对基本类型的数组从小到大排序:
sort ( 数组名 + n1 , 数组名 + n2) ;
n1 和 n2 都是 int 类型得表达式
如果n1 = 0,则 + n1可以不写
将数组中下标范围为[n1 , n2) 的元素从小到大排序.
example:
Cout函数为自写程序用于输出数组结果
int a[] = {
15,4,3,9,7,2,6 };
sort(a, a + 7); // 对整个数组从小到大排列
Cout(a); // 结果 : 2 3 4 6 7 9 15
int b[] = {
15,4,3,9,7,2,6 };
sort(b, b + 3); // 排序从b0到b2
Cout(b); // 结果 : 3 4 15 9 7 2 6
int c[] = {
15,4,3,9,7,2,6 };
sort(c + 2, c + 5); // 排序从c2到c4
Cout(c); // 结果 : 15 4 3 7 9 2 6
Usage two (sort from big to small/from small to big for the basic type array with element type T)
用法: sort( 数组名 + n1 , 数组名 + n2 , greater<T>() );
用法: sort( 数组名 + n1 , 数组名 + n2 , less<T>() );
greater and less are the built-in sorting templates from largest to smallest and smallest to largest in C++ respectively; *header file*
// function 头文件中定义了大量模板,用来声明函数对象
// greater 和 less是其中的两个常用模板
template <class T> struct greater {
bool operator() (const T& x, const T& y) const {
return x>y;}
typedef T first_argument_type;
typedef T second_argument_type;
typedef bool result_type;
};
template <class T> struct less {
bool operator() (const T& x, const T& y) const {
return x<y;}
typedef T first_argument_type;
typedef T second_argument_type;
typedef bool result_type;
};
example:
int a[] = {
15,4,3,9,7,2,6 };
sort(a + 1, a + 4, greater<int>()); // 从大到小排序 int类型数组a 从a1到a3
Cout(a); // 结果: 15 9 4 3 7 2 6
int b[] = {
15,4,3,9,7,2,6 };
sort(b + 1, b + 4, less<int>()); // 从小到大排序 int类型数组a 从b1到b3
Cout(b); // 结果: 15 3 4 9 7 2 6
Usage three (custom sorting rules)
- Use custom sorting rules to sort arrays of any type T;
用法: sort(数组名 + n1 , 数组名 + n2 , 排序规则结构名() );
- How the collation structure is defined:
struct 结构名
{
bool operator()( const T & a1,const T & a2) const {
// 若a1应该在a2前面, 则返回true
// 否则返回false
}
}
example:
- Create a template:
struct Rule1 // 按从大到小排序
{
bool operator()(const int& a1, const int& a2) const {
return a1 > a2; //正确返回true 错误返回false,为true是a1应该在前面,那么就保证了大的数排在前面
}
};
struct Rule2 // 按个位数大小排序
{
bool operator()(const int& a1, const int& a2)const {
return a1 % 10 < a2 % 10;
}
};
- Call template:
int main() {
int a[] = {
12,45,3,98,21,7 };
sort(a, a + sizeof(a) / sizeof(int)); // 从小到大排序整个数组
Cout(a); // 结果: 3 7 12 21 45 98
sort(a, a + sizeof(a) / sizeof(int), Rule1()); // 调用Rule1模板 从大到小排序
Cout(a); // 结果: 98 45 21 12 7 3
sort(a, a + sizeof(a) / sizeof(int), Rule2()); // 调用Rule2模板 按个位数从小到大排序
Cout(a); // 结果: 21 12 3 45 7 98
return 0;
}
Usage four (use sort to sort the structure array)
- Structure initialization:
struct Student {
char name[20];
int id;
double gpa;
};
Student Students[] = {
{
"Jack",112,3.4},{
"Mary",102,3.8},{
"Kate",117,3.9},
{
"Ala",333,3.5},{
"Zero",101,4.0} };
- Define structure sorting template
#pragma warning(disable:4996) // vs2019认为stricmp不安全,使用stricmp前加上此代码可忽略
struct StudentRule1 {
//按姓名从小到大排序
bool operator()(const Student& s1, const Student& s2)const {
if (stricmp(s1.name, s2.name) < 0)
return true;
return false;
}
};
struct StudentRule2 {
//按id从小到大排序
bool operator()(const Student& s1, const Student& s2)const {
return s1.id < s2.id;
}
};
struct StudentRule3 {
//按gpa从高到低排序
bool operator()(const Student& s1, const Student& s2)const {
return s1.gpa > s2.gpa;
}
};
- Output function definition and declaration
void PrintStudents(Student s[], int size);
void PrintStudents(Student s[], int size) {
for (int i = 0; i < size; i++)
cout << " (" << s[i].name << "," << s[i].id << "," << s[i].gpa << ") ";
cout << endl;
}
- Output sort result
int main() {
int n = sizeof(Students) / sizeof(Student);
sort(Students, Students + n, StudentRule1()); //按名字从小到大排序
PrintStudents(Students, n);
//结果:(A1a,333,3.5) (Jack,112,3.4) (Mary,102,3.8) (Mary,117,3.9) (Zero,101,4)
sort(Students, Students + n, StudentRule2()); //按id从小到大排序
PrintStudents(Students, n);
//结果:(Zero,101,4) (Mary,102,3.8) (Jack,112,3.4) (Mary,117,3.9) (Ala,333,3.5)
sort(Students, Students + n, StudentRule3()); //按gpa从高到低排序
PrintStudents(Students, n);
//结果:(Zero,101,4) (Kate,117,3.9) (Mary,102,3.8) (Ala,333,3.5) (Jack,112,3.4)
return 0;
}
Followed by the next content: STL series (two) (three) (four) (five)
pointer series...
just pay attention and go!