第十一周(笔记)
11.1 STL初步
STL概述
STL:(Standard Template Library) 标准模板库 包含一些常用的算法如排序查找,还有常用的数据结构如可变长数组、链表、字典等。 使用方便,效率较高 要使用其中的算法,需要**#include < algorithm >** 用sort进行排序(用法一)
对基本类型的数组从小到大排序 :sort(数组名+n1,数组名+n2);
n1和n2都是int类型的表达式,可以包含变量 如果n1 = 0,则+n1可以不写 将数组中下标范围为[n1,n2)的元素从小到大排序。下标为n2的元素不在排序区间内
# include <iostream>
# include <algorithm>
using namespace std;
int main ( )
{
int a[ ] = { 15 , 4 , 3 , 9 , 7 , 2 , 6 } ;
sort ( a+ 2 , a+ 5 ) ;
int len = sizeof ( a) / sizeof ( int ) ;
cout << len << endl;
int i = 0 ;
while ( i< len) {
cout << a[ i] ;
i++ ;
}
return 0 ;
}
用sort进行排序(用法二)
对元素类型为T的基本类型数组从大到小排序 :sort(数组名+n1,数组名+n2,greater< T >());
# include <algorithm>
# include <iostream>
using namespace std;
int main ( ) {
int a[ ] = { 15 , 4 , 3 , 9 , 7 , 2 , 6 } ;
sort ( a + 1 , a + 4 , greater< int > ( ) ) ;
int len = sizeof ( a) / sizeof ( int ) ;
cout << len << endl;
int i = 0 ;
while ( i < len) {
cout << a[ i] ;
i++ ;
}
return 0 ;
}
用sort进行排序(用法三)
用自定义的排序规则 ,对任何类型T的数组排序:sort(数组名+n1,数组名+n2,排序规则结构名()) ;排序规则定义方式
struct 结构名 {
bool operator ( ) ( const T & a1, const T & a2) const {
}
}
# include <algorithm>
# include <iostream>
using namespace std;
struct Rule1 {
bool operator ( ) ( const int & a1, const int & a2) const { return a1 > a2; }
} ;
struct Rule2 {
bool operator ( ) ( const int & a1, const int & a2) const {
return a1 % 10 < a2 % 10 ;
}
} ;
void Print ( int a[ ] , int size) {
for ( int i = 0 ; i < size; i++ ) cout << a[ i] << "," ;
cout << endl;
}
int main ( ) {
int a[ ] = { 12 , 45 , 3 , 9 , 8 , 21 , 7 } ;
sort ( a, a + sizeof ( a) / sizeof ( int ) ) ;
cout << "1) " ;
Print ( a, sizeof ( a) / sizeof ( int ) ) ;
sort ( a, a + sizeof ( a) / sizeof ( int ) , Rule1 ( ) ) ;
cout << "2) " ;
Print ( a, sizeof ( a) / sizeof ( int ) ) ;
sort ( a, a + sizeof ( a) / sizeof ( int ) , Rule2 ( ) ) ;
cout << "3) " ;
Print ( a, sizeof ( a) / sizeof ( int ) ) ;
return 0 ;
}
用sort对结构数组进行排序
# include <algorithm>
# include <cstring>
# include <iostream>
using namespace std;
struct Student {
char name[ 20 ] ;
int id;
double gpa;
} ;
Student students[ ] = { { "jack" , 112 , 3.4 } ,
{ "mary" , 102 , 3.8 } ,
{ "alaa" , 333 , 3.5 } ,
{ "zero" , 101 , 4.0 } } ;
struct StudentRule1 {
bool operator ( ) ( const Student& s1, const Student& s2) const {
if ( stricmp ( s1. name, s2. name) < 0 ) return true ;
return false ;
}
} ;
struct StudentRule2 {
bool operator ( ) ( const Student& s1, const Student& s2) const {
return s1. id < s2. id;
}
} ;
struct StudentRule3 {
bool operator ( ) ( const Student& s1, const Student& s2) const {
return s1. gpa > s2. gpa;
}
} ;
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;
}
int main ( ) {
int n = sizeof ( students) / sizeof ( Student) ;
sort ( students, students + n, StudentRule1 ( ) ) ;
PrintStudents ( students, n) ;
sort ( students, students + n, StudentRule2 ( ) ) ;
PrintStudents ( students, n) ;
sort ( students, students + n, StudentRule3 ( ) ) ;
PrintStudents ( students, n) ;
return 0 ;
}