strlen() 字符串长度
strcmp() 字符串比较
strcpy() 字符串拷贝
memset() 暴力清空
memcpy() 暴力拷贝
三角函数、指数函数、浮点取整函数
qsort() C语言快排
rand() 随机数
malloc() free() C语言动态分配内存
time(0) 从1970年到现在的秒数(配合随机数)
clock() 程序启动到目前位置的毫秒数
isdigit(), isalpha(),判断字符是否为数字、大小写字母
//生成随机数 #include #include #include using namespace std; int main(){ srand(time(NULL));//设置随机数种子 rand() % 100;//生成一个[0,100)的随机数 return 0; }
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-2LFJaH68-1633739580471)()]
//vector的创建 #include using namespace std; int main() { vectorv1; //创建一个存int类型的动态数组,int可以改成其它类型 vectorv2{1,1,2,3,5,8}; //创建一个存double类型的动态数组,长度为6,1 1 2 3 5 8分别存在v[0]~v[5] vectorv3(20); //创建一个存long long类型的动态数组,长度为20,v[0]~v[19]默认为0 vectorv4(20,"zzuacm"); //创建一个存string类型的动态数组,长度为20,存的都是"zzuacm" vectorv5[3]; //相当于存int的二维数组,一共3行,每行的列可变 vector >v5{{1,2},{1},{1,2,3}}; //存int的二维数组,行和列都可变,初始状态 return 0; }
int main() { vectorv; for(int i=1;i<=5;i++) v.push_back(i);//向动态数组中插入1~5 cout< 删除 遍历 int main() { vectorv{0,1,2,3,4}; v.erase(v.begin()+3);//删除v[3],v变为{0,1,2,4} v.insert(v.begin()+3,666);//在v[3]前加上666,v变成{0,1,2,666,4} v.front()=10;//将v[0]改成10,等同于v[0]=10; v.back()=20;//将v[4]改成20等同于v[v.size()-1]=20; //下标遍历 for(int i=0;i::iterator it=v.begin();it!=v.end();it++) cout<<*it<<" ";//使用迭代器,从v.begin()到v.end()-1 for(auto i=v.begin();i!=v.end();i++) cout<<*i<<" ";//使用迭代器,从v.begin()到v.end()-1 cout< 迭代器 vector::iterator it=v.begin(); //很奇怪的数据类型(对应STL的指针) auto it=v.begin(); 在这⾥ it 类似于⼀个指针,指向 v 的第⼀个元素 it 等价于 &v[0] *it 等价于 v[0] it 也可以进⾏加减操作,例如 it + 3 指向第四个元素 it++ 后it指向的就是v的第二个元素(v[1])了 字符串 成员函数 所有参数为字符串的地方既可以是string也可以是c字符串字符串操作与vector类似,但size,length复杂度较高可通过下标访问字符串元素 加速读取 #include //#include using namespace std; int main() { string a; char ch[100]; scanf("%s",ch); a = string(ch); cout< 栈 是一种线性结构 成员函数 头文件 队列 成员函数 优先队列 优先队列是按照优先级出列的。每次的首元素都是优先级最大的。 优先队列的优先级是以定义的**运算符 <**来说,最大的那个元素。 q.top()获取队列首位(最大)的元素 注意运算符的重载 map 内置pair,见后 成员函数 遍历 #include #include using namespace std; int main() { mapdata; data["星期天"] = 7; data["星期六"] = 6; data.insert(pair("星期五",5)); for(map::iterator it = data.begin();it!=data.end();it++){ cout<< it->first <<" "<< it->second < 注意 有的题目在使用map时会卡时间,原因是map的访问添加都是O(nlogn)。遇到这种情况,只需要使用unordered_map,unordered_map 的访问添加是O(1) 。除了初始化时写成unordered_map<键类型,值类型>变量名外,其他的操作都是一样的。map是有序的,unordered_map是无序的 pair 简易版struct 构造 #define pii pair //#define x first //#define y second using pii = pair; typdef pair pii; int main(){ pii a (1, 2); pii b = make_pair(3, 4); pair c ("name", make_pair(1, 2)); auto d = c; return 0; } set 集合(set)是一种包含对象的容器,可以快速地(logn)查询元素是否在已知几集合中。set 中所有元素是有序地,且只能出现⼀次,因为 set 中元素是有序的,所以存储的元素必须已经定义 过「<」运算符(因此如果想在 set 中存放 struct 的话必须⼿动重载「<」运算符,和优先队列一样)与set类似的还有 multiset元素有序可以出现多次unordered_set元素无序只能出现一次unordered_multiset元素无序可以出现多次 成员函数 建立与遍历 //建立方法: sets; multisets; unorded_sets; unorded_multisets; //如果Type无法进行比较,还需要和优先队列一样定义<运算符 //遍历方法: for(auto i:s)cout< 查找元素 sets; if(s.find(666) == s.end()){ cout << "666 was not in set"; } else{ cout << *(s.find(666)); } 注意 int main(){ set s; auto i = s.begin(); i++, i++, i++, i++; //i += 4; 错误 cout << *i; return 0; } 重载比较 set 容器模版需要3个泛型参数,如下:template class set; 第一个T 是元素类型,必选; 第二个C 指定元素比较方式,缺省为 Less, 即使用 < 符号比较; 第三个A 指定空间分配对象,一般使用默认类型。 因此: (1) 如果第2个泛型参数你使用默认值的话,你的自定义元素类型需要重载 < 运算操作; (2)如果你第2个泛型参数不使用默认值的话,则比较对象必须具有 () 操作,即: bool operator()(const T &a, const T &b) #include #include #include using namespace std; int m,k; struct cmp{ bool operator() (int a,int b){ if(abs(a-b)<=k){ return false; } else{ return a s; char op[10]; int x; int main(void){ scanf("%d%d",&m,&k); while(m--){ scanf("%s%d",op,&x); if(op[0]=='a'){ if(s.find(x)==s.end()){ s.insert(x); } } else if(op[0]=='d'){ s.erase(x); } else{ if(s.find(x)!=s.end()){ puts("Yes"); } else{ puts("No"); } } } return 0; } algorithm sort() sort(first, last, compare) first:排序起始位置(指针或 iterator)last:排序终⽌位置(指针或 iterator)compare:⽐较⽅式,可以省略,省略时默认按升序排序,如果排序的元素没有定义比较运算(如结构体),必须有comparesort 排序的范围是 [first, last),时间为 O(nlogn)作用:使指定容器范围内的元素有序,默认从小到大排序。可以排序所有已经定义的数据类型 cmp() 对于未定义 < 小于号的 数据类型,可以写一个cmp函数来定义排序的规则。 同样的,基本数据类型也可以通过cmp来自定义排序规则。 可更改升降序 #include #include #include #include using namespace std; const int n = 10; struct st{ int A_score; int B_score; }; st stu[n+5]; void putt();//输出stu //降序 bool cmp(st a,st b){//两个关键词的排序 if(a.A_score != b.A_score) return a.A_score > b.A_score; return a.B_score > b.B_score; } //升序 bool cmp(st a,st b){//两个关键词的排序 if(a.A_score != b.A_score) return a.A_score < b.A_score; return a.B_score < b.B_score; } int main() { srand(time(NULL)); for(int i = 0;i < n;i++){ stu[i].A_score = rand()%5 + 1; stu[i].B_score = rand()%5 + 1; } cout<<"排序前:"< 重载< 重载结构体的排序规则 #include #include #include #include using namespace std; const int n = 10; struct st{ int A_score; int B_score; bool operator < (const st b){ if(this->A_score != b.A_score) return this->A_score < b.A_score; return this->B_score < b.B_score; } }; st stu[n+5]; void putt();//输出stu int main() { srand(time(NULL)); for(int i = 0;i < n;i++){ stu[i].A_score = rand()%5 + 1; stu[i].B_score = rand()%5 + 1; } cout<<"排序前:"< next_permutation() 作用:用于求序列[first,last)元素全排列中一个排序的下一个排序 #include using namespace std; int main() { int a[3] = {0, 1, 2}; do { for (int i = 0; i < 3; i++) cout< 返回值:如果有下一个排列 则返回1,否则返回0。 二分函数 lower_bound(first, last, value) ▸ first:查找起始位置(指针或 iterator)▸ last:查找终⽌位置(指针或 iterator)▸ value:查找的值▸ lower_bound 查找的范围是 [first, last),返回的是序列中第⼀个大于等于 value 的元素的位置,时间为 O(logn)▸ [first, last)范围内的序列必须是提前排好序的,不然会错▸ 如果序列内所有元素都⽐ value ⼩,则返回last upper_bound(first, last, value) ▸ upper_bound 与 lower_bound 相似,唯⼀不同的地⽅在于upper_bound 查找的是序列中第⼀个⼤于 value 的元素 int main() { int arr[]={3,2,5,1,4}; sort(arr,arr+5);//需要先排序 cout << *lower_bound(arr,arr+5,3);//输出数组中第一个大于等于3的值 return 0; } unique() 去重函数(unique) unique(first, last): ▸ [first, last)范围内的值必须是一开始就提前排好序的▸ 移除 [first, last) 内连续重复项▸ 返回值:去重之后的返回最后一个元素的下一个地址(迭代器) #include using namespace std; int main() { int arr[]={3,2,2,1,2},n; sort(arr,arr+5);//需要先排序 n=unique(arr,arr+5)-arr;//n是去重后的元素个数 return 0; } reverse() 可反转容器等 string str="hello world , hi"; reverse(str.begin(),str.end());//str结果为 ih , dlrow olleh vector v = {5,4,3,2,1}; reverse(v.begin(),v.end());//容器v的值变为1,2,3,4,5 max() min() swap() 交换指针,可用于容器
int main() { vectorv{0,1,2,3,4}; v.erase(v.begin()+3);//删除v[3],v变为{0,1,2,4} v.insert(v.begin()+3,666);//在v[3]前加上666,v变成{0,1,2,666,4} v.front()=10;//将v[0]改成10,等同于v[0]=10; v.back()=20;//将v[4]改成20等同于v[v.size()-1]=20; //下标遍历 for(int i=0;i::iterator it=v.begin();it!=v.end();it++) cout<<*it<<" ";//使用迭代器,从v.begin()到v.end()-1 for(auto i=v.begin();i!=v.end();i++) cout<<*i<<" ";//使用迭代器,从v.begin()到v.end()-1 cout< 迭代器 vector::iterator it=v.begin(); //很奇怪的数据类型(对应STL的指针) auto it=v.begin(); 在这⾥ it 类似于⼀个指针,指向 v 的第⼀个元素 it 等价于 &v[0] *it 等价于 v[0] it 也可以进⾏加减操作,例如 it + 3 指向第四个元素 it++ 后it指向的就是v的第二个元素(v[1])了 字符串 成员函数 所有参数为字符串的地方既可以是string也可以是c字符串字符串操作与vector类似,但size,length复杂度较高可通过下标访问字符串元素 加速读取 #include //#include using namespace std; int main() { string a; char ch[100]; scanf("%s",ch); a = string(ch); cout< 栈 是一种线性结构 成员函数 头文件 队列 成员函数 优先队列 优先队列是按照优先级出列的。每次的首元素都是优先级最大的。 优先队列的优先级是以定义的**运算符 <**来说,最大的那个元素。 q.top()获取队列首位(最大)的元素 注意运算符的重载 map 内置pair,见后 成员函数 遍历 #include #include using namespace std; int main() { mapdata; data["星期天"] = 7; data["星期六"] = 6; data.insert(pair("星期五",5)); for(map::iterator it = data.begin();it!=data.end();it++){ cout<< it->first <<" "<< it->second < 注意 有的题目在使用map时会卡时间,原因是map的访问添加都是O(nlogn)。遇到这种情况,只需要使用unordered_map,unordered_map 的访问添加是O(1) 。除了初始化时写成unordered_map<键类型,值类型>变量名外,其他的操作都是一样的。map是有序的,unordered_map是无序的 pair 简易版struct 构造 #define pii pair //#define x first //#define y second using pii = pair; typdef pair pii; int main(){ pii a (1, 2); pii b = make_pair(3, 4); pair c ("name", make_pair(1, 2)); auto d = c; return 0; } set 集合(set)是一种包含对象的容器,可以快速地(logn)查询元素是否在已知几集合中。set 中所有元素是有序地,且只能出现⼀次,因为 set 中元素是有序的,所以存储的元素必须已经定义 过「<」运算符(因此如果想在 set 中存放 struct 的话必须⼿动重载「<」运算符,和优先队列一样)与set类似的还有 multiset元素有序可以出现多次unordered_set元素无序只能出现一次unordered_multiset元素无序可以出现多次 成员函数 建立与遍历 //建立方法: sets; multisets; unorded_sets; unorded_multisets; //如果Type无法进行比较,还需要和优先队列一样定义<运算符 //遍历方法: for(auto i:s)cout< 查找元素 sets; if(s.find(666) == s.end()){ cout << "666 was not in set"; } else{ cout << *(s.find(666)); } 注意 int main(){ set s; auto i = s.begin(); i++, i++, i++, i++; //i += 4; 错误 cout << *i; return 0; } 重载比较 set 容器模版需要3个泛型参数,如下:template class set; 第一个T 是元素类型,必选; 第二个C 指定元素比较方式,缺省为 Less, 即使用 < 符号比较; 第三个A 指定空间分配对象,一般使用默认类型。 因此: (1) 如果第2个泛型参数你使用默认值的话,你的自定义元素类型需要重载 < 运算操作; (2)如果你第2个泛型参数不使用默认值的话,则比较对象必须具有 () 操作,即: bool operator()(const T &a, const T &b) #include #include #include using namespace std; int m,k; struct cmp{ bool operator() (int a,int b){ if(abs(a-b)<=k){ return false; } else{ return a s; char op[10]; int x; int main(void){ scanf("%d%d",&m,&k); while(m--){ scanf("%s%d",op,&x); if(op[0]=='a'){ if(s.find(x)==s.end()){ s.insert(x); } } else if(op[0]=='d'){ s.erase(x); } else{ if(s.find(x)!=s.end()){ puts("Yes"); } else{ puts("No"); } } } return 0; } algorithm sort() sort(first, last, compare) first:排序起始位置(指针或 iterator)last:排序终⽌位置(指针或 iterator)compare:⽐较⽅式,可以省略,省略时默认按升序排序,如果排序的元素没有定义比较运算(如结构体),必须有comparesort 排序的范围是 [first, last),时间为 O(nlogn)作用:使指定容器范围内的元素有序,默认从小到大排序。可以排序所有已经定义的数据类型 cmp() 对于未定义 < 小于号的 数据类型,可以写一个cmp函数来定义排序的规则。 同样的,基本数据类型也可以通过cmp来自定义排序规则。 可更改升降序 #include #include #include #include using namespace std; const int n = 10; struct st{ int A_score; int B_score; }; st stu[n+5]; void putt();//输出stu //降序 bool cmp(st a,st b){//两个关键词的排序 if(a.A_score != b.A_score) return a.A_score > b.A_score; return a.B_score > b.B_score; } //升序 bool cmp(st a,st b){//两个关键词的排序 if(a.A_score != b.A_score) return a.A_score < b.A_score; return a.B_score < b.B_score; } int main() { srand(time(NULL)); for(int i = 0;i < n;i++){ stu[i].A_score = rand()%5 + 1; stu[i].B_score = rand()%5 + 1; } cout<<"排序前:"< 重载< 重载结构体的排序规则 #include #include #include #include using namespace std; const int n = 10; struct st{ int A_score; int B_score; bool operator < (const st b){ if(this->A_score != b.A_score) return this->A_score < b.A_score; return this->B_score < b.B_score; } }; st stu[n+5]; void putt();//输出stu int main() { srand(time(NULL)); for(int i = 0;i < n;i++){ stu[i].A_score = rand()%5 + 1; stu[i].B_score = rand()%5 + 1; } cout<<"排序前:"< next_permutation() 作用:用于求序列[first,last)元素全排列中一个排序的下一个排序 #include using namespace std; int main() { int a[3] = {0, 1, 2}; do { for (int i = 0; i < 3; i++) cout< 返回值:如果有下一个排列 则返回1,否则返回0。 二分函数 lower_bound(first, last, value) ▸ first:查找起始位置(指针或 iterator)▸ last:查找终⽌位置(指针或 iterator)▸ value:查找的值▸ lower_bound 查找的范围是 [first, last),返回的是序列中第⼀个大于等于 value 的元素的位置,时间为 O(logn)▸ [first, last)范围内的序列必须是提前排好序的,不然会错▸ 如果序列内所有元素都⽐ value ⼩,则返回last upper_bound(first, last, value) ▸ upper_bound 与 lower_bound 相似,唯⼀不同的地⽅在于upper_bound 查找的是序列中第⼀个⼤于 value 的元素 int main() { int arr[]={3,2,5,1,4}; sort(arr,arr+5);//需要先排序 cout << *lower_bound(arr,arr+5,3);//输出数组中第一个大于等于3的值 return 0; } unique() 去重函数(unique) unique(first, last): ▸ [first, last)范围内的值必须是一开始就提前排好序的▸ 移除 [first, last) 内连续重复项▸ 返回值:去重之后的返回最后一个元素的下一个地址(迭代器) #include using namespace std; int main() { int arr[]={3,2,2,1,2},n; sort(arr,arr+5);//需要先排序 n=unique(arr,arr+5)-arr;//n是去重后的元素个数 return 0; } reverse() 可反转容器等 string str="hello world , hi"; reverse(str.begin(),str.end());//str结果为 ih , dlrow olleh vector v = {5,4,3,2,1}; reverse(v.begin(),v.end());//容器v的值变为1,2,3,4,5 max() min() swap() 交换指针,可用于容器
vector::iterator it=v.begin(); //很奇怪的数据类型(对应STL的指针) auto it=v.begin();
在这⾥ it 类似于⼀个指针,指向 v 的第⼀个元素
it 等价于 &v[0]
*it 等价于 v[0]
it 也可以进⾏加减操作,例如 it + 3 指向第四个元素
it++ 后it指向的就是v的第二个元素(v[1])了
#include //#include using namespace std; int main() { string a; char ch[100]; scanf("%s",ch); a = string(ch); cout< 栈 是一种线性结构 成员函数 头文件 队列 成员函数 优先队列 优先队列是按照优先级出列的。每次的首元素都是优先级最大的。 优先队列的优先级是以定义的**运算符 <**来说,最大的那个元素。 q.top()获取队列首位(最大)的元素 注意运算符的重载 map 内置pair,见后 成员函数 遍历 #include #include using namespace std; int main() { mapdata; data["星期天"] = 7; data["星期六"] = 6; data.insert(pair("星期五",5)); for(map::iterator it = data.begin();it!=data.end();it++){ cout<< it->first <<" "<< it->second < 注意 有的题目在使用map时会卡时间,原因是map的访问添加都是O(nlogn)。遇到这种情况,只需要使用unordered_map,unordered_map 的访问添加是O(1) 。除了初始化时写成unordered_map<键类型,值类型>变量名外,其他的操作都是一样的。map是有序的,unordered_map是无序的 pair 简易版struct 构造 #define pii pair //#define x first //#define y second using pii = pair; typdef pair pii; int main(){ pii a (1, 2); pii b = make_pair(3, 4); pair c ("name", make_pair(1, 2)); auto d = c; return 0; } set 集合(set)是一种包含对象的容器,可以快速地(logn)查询元素是否在已知几集合中。set 中所有元素是有序地,且只能出现⼀次,因为 set 中元素是有序的,所以存储的元素必须已经定义 过「<」运算符(因此如果想在 set 中存放 struct 的话必须⼿动重载「<」运算符,和优先队列一样)与set类似的还有 multiset元素有序可以出现多次unordered_set元素无序只能出现一次unordered_multiset元素无序可以出现多次 成员函数 建立与遍历 //建立方法: sets; multisets; unorded_sets; unorded_multisets; //如果Type无法进行比较,还需要和优先队列一样定义<运算符 //遍历方法: for(auto i:s)cout< 查找元素 sets; if(s.find(666) == s.end()){ cout << "666 was not in set"; } else{ cout << *(s.find(666)); } 注意 int main(){ set s; auto i = s.begin(); i++, i++, i++, i++; //i += 4; 错误 cout << *i; return 0; } 重载比较 set 容器模版需要3个泛型参数,如下:template class set; 第一个T 是元素类型,必选; 第二个C 指定元素比较方式,缺省为 Less, 即使用 < 符号比较; 第三个A 指定空间分配对象,一般使用默认类型。 因此: (1) 如果第2个泛型参数你使用默认值的话,你的自定义元素类型需要重载 < 运算操作; (2)如果你第2个泛型参数不使用默认值的话,则比较对象必须具有 () 操作,即: bool operator()(const T &a, const T &b) #include #include #include using namespace std; int m,k; struct cmp{ bool operator() (int a,int b){ if(abs(a-b)<=k){ return false; } else{ return a s; char op[10]; int x; int main(void){ scanf("%d%d",&m,&k); while(m--){ scanf("%s%d",op,&x); if(op[0]=='a'){ if(s.find(x)==s.end()){ s.insert(x); } } else if(op[0]=='d'){ s.erase(x); } else{ if(s.find(x)!=s.end()){ puts("Yes"); } else{ puts("No"); } } } return 0; } algorithm sort() sort(first, last, compare) first:排序起始位置(指针或 iterator)last:排序终⽌位置(指针或 iterator)compare:⽐较⽅式,可以省略,省略时默认按升序排序,如果排序的元素没有定义比较运算(如结构体),必须有comparesort 排序的范围是 [first, last),时间为 O(nlogn)作用:使指定容器范围内的元素有序,默认从小到大排序。可以排序所有已经定义的数据类型 cmp() 对于未定义 < 小于号的 数据类型,可以写一个cmp函数来定义排序的规则。 同样的,基本数据类型也可以通过cmp来自定义排序规则。 可更改升降序 #include #include #include #include using namespace std; const int n = 10; struct st{ int A_score; int B_score; }; st stu[n+5]; void putt();//输出stu //降序 bool cmp(st a,st b){//两个关键词的排序 if(a.A_score != b.A_score) return a.A_score > b.A_score; return a.B_score > b.B_score; } //升序 bool cmp(st a,st b){//两个关键词的排序 if(a.A_score != b.A_score) return a.A_score < b.A_score; return a.B_score < b.B_score; } int main() { srand(time(NULL)); for(int i = 0;i < n;i++){ stu[i].A_score = rand()%5 + 1; stu[i].B_score = rand()%5 + 1; } cout<<"排序前:"< 重载< 重载结构体的排序规则 #include #include #include #include using namespace std; const int n = 10; struct st{ int A_score; int B_score; bool operator < (const st b){ if(this->A_score != b.A_score) return this->A_score < b.A_score; return this->B_score < b.B_score; } }; st stu[n+5]; void putt();//输出stu int main() { srand(time(NULL)); for(int i = 0;i < n;i++){ stu[i].A_score = rand()%5 + 1; stu[i].B_score = rand()%5 + 1; } cout<<"排序前:"< next_permutation() 作用:用于求序列[first,last)元素全排列中一个排序的下一个排序 #include using namespace std; int main() { int a[3] = {0, 1, 2}; do { for (int i = 0; i < 3; i++) cout< 返回值:如果有下一个排列 则返回1,否则返回0。 二分函数 lower_bound(first, last, value) ▸ first:查找起始位置(指针或 iterator)▸ last:查找终⽌位置(指针或 iterator)▸ value:查找的值▸ lower_bound 查找的范围是 [first, last),返回的是序列中第⼀个大于等于 value 的元素的位置,时间为 O(logn)▸ [first, last)范围内的序列必须是提前排好序的,不然会错▸ 如果序列内所有元素都⽐ value ⼩,则返回last upper_bound(first, last, value) ▸ upper_bound 与 lower_bound 相似,唯⼀不同的地⽅在于upper_bound 查找的是序列中第⼀个⼤于 value 的元素 int main() { int arr[]={3,2,5,1,4}; sort(arr,arr+5);//需要先排序 cout << *lower_bound(arr,arr+5,3);//输出数组中第一个大于等于3的值 return 0; } unique() 去重函数(unique) unique(first, last): ▸ [first, last)范围内的值必须是一开始就提前排好序的▸ 移除 [first, last) 内连续重复项▸ 返回值:去重之后的返回最后一个元素的下一个地址(迭代器) #include using namespace std; int main() { int arr[]={3,2,2,1,2},n; sort(arr,arr+5);//需要先排序 n=unique(arr,arr+5)-arr;//n是去重后的元素个数 return 0; } reverse() 可反转容器等 string str="hello world , hi"; reverse(str.begin(),str.end());//str结果为 ih , dlrow olleh vector v = {5,4,3,2,1}; reverse(v.begin(),v.end());//容器v的值变为1,2,3,4,5 max() min() swap() 交换指针,可用于容器
是一种线性结构
头文件
优先队列是按照优先级出列的。每次的首元素都是优先级最大的。
优先队列的优先级是以定义的**运算符 <**来说,最大的那个元素。
q.top()获取队列首位(最大)的元素
注意运算符的重载
内置pair,见后
#include #include using namespace std; int main() { mapdata; data["星期天"] = 7; data["星期六"] = 6; data.insert(pair("星期五",5)); for(map::iterator it = data.begin();it!=data.end();it++){ cout<< it->first <<" "<< it->second < 注意 有的题目在使用map时会卡时间,原因是map的访问添加都是O(nlogn)。遇到这种情况,只需要使用unordered_map,unordered_map 的访问添加是O(1) 。除了初始化时写成unordered_map<键类型,值类型>变量名外,其他的操作都是一样的。map是有序的,unordered_map是无序的 pair 简易版struct 构造 #define pii pair //#define x first //#define y second using pii = pair; typdef pair pii; int main(){ pii a (1, 2); pii b = make_pair(3, 4); pair c ("name", make_pair(1, 2)); auto d = c; return 0; } set 集合(set)是一种包含对象的容器,可以快速地(logn)查询元素是否在已知几集合中。set 中所有元素是有序地,且只能出现⼀次,因为 set 中元素是有序的,所以存储的元素必须已经定义 过「<」运算符(因此如果想在 set 中存放 struct 的话必须⼿动重载「<」运算符,和优先队列一样)与set类似的还有 multiset元素有序可以出现多次unordered_set元素无序只能出现一次unordered_multiset元素无序可以出现多次 成员函数 建立与遍历 //建立方法: sets; multisets; unorded_sets; unorded_multisets; //如果Type无法进行比较,还需要和优先队列一样定义<运算符 //遍历方法: for(auto i:s)cout< 查找元素 sets; if(s.find(666) == s.end()){ cout << "666 was not in set"; } else{ cout << *(s.find(666)); } 注意 int main(){ set s; auto i = s.begin(); i++, i++, i++, i++; //i += 4; 错误 cout << *i; return 0; } 重载比较 set 容器模版需要3个泛型参数,如下:template class set; 第一个T 是元素类型,必选; 第二个C 指定元素比较方式,缺省为 Less, 即使用 < 符号比较; 第三个A 指定空间分配对象,一般使用默认类型。 因此: (1) 如果第2个泛型参数你使用默认值的话,你的自定义元素类型需要重载 < 运算操作; (2)如果你第2个泛型参数不使用默认值的话,则比较对象必须具有 () 操作,即: bool operator()(const T &a, const T &b) #include #include #include using namespace std; int m,k; struct cmp{ bool operator() (int a,int b){ if(abs(a-b)<=k){ return false; } else{ return a s; char op[10]; int x; int main(void){ scanf("%d%d",&m,&k); while(m--){ scanf("%s%d",op,&x); if(op[0]=='a'){ if(s.find(x)==s.end()){ s.insert(x); } } else if(op[0]=='d'){ s.erase(x); } else{ if(s.find(x)!=s.end()){ puts("Yes"); } else{ puts("No"); } } } return 0; } algorithm sort() sort(first, last, compare) first:排序起始位置(指针或 iterator)last:排序终⽌位置(指针或 iterator)compare:⽐较⽅式,可以省略,省略时默认按升序排序,如果排序的元素没有定义比较运算(如结构体),必须有comparesort 排序的范围是 [first, last),时间为 O(nlogn)作用:使指定容器范围内的元素有序,默认从小到大排序。可以排序所有已经定义的数据类型 cmp() 对于未定义 < 小于号的 数据类型,可以写一个cmp函数来定义排序的规则。 同样的,基本数据类型也可以通过cmp来自定义排序规则。 可更改升降序 #include #include #include #include using namespace std; const int n = 10; struct st{ int A_score; int B_score; }; st stu[n+5]; void putt();//输出stu //降序 bool cmp(st a,st b){//两个关键词的排序 if(a.A_score != b.A_score) return a.A_score > b.A_score; return a.B_score > b.B_score; } //升序 bool cmp(st a,st b){//两个关键词的排序 if(a.A_score != b.A_score) return a.A_score < b.A_score; return a.B_score < b.B_score; } int main() { srand(time(NULL)); for(int i = 0;i < n;i++){ stu[i].A_score = rand()%5 + 1; stu[i].B_score = rand()%5 + 1; } cout<<"排序前:"< 重载< 重载结构体的排序规则 #include #include #include #include using namespace std; const int n = 10; struct st{ int A_score; int B_score; bool operator < (const st b){ if(this->A_score != b.A_score) return this->A_score < b.A_score; return this->B_score < b.B_score; } }; st stu[n+5]; void putt();//输出stu int main() { srand(time(NULL)); for(int i = 0;i < n;i++){ stu[i].A_score = rand()%5 + 1; stu[i].B_score = rand()%5 + 1; } cout<<"排序前:"< next_permutation() 作用:用于求序列[first,last)元素全排列中一个排序的下一个排序 #include using namespace std; int main() { int a[3] = {0, 1, 2}; do { for (int i = 0; i < 3; i++) cout< 返回值:如果有下一个排列 则返回1,否则返回0。 二分函数 lower_bound(first, last, value) ▸ first:查找起始位置(指针或 iterator)▸ last:查找终⽌位置(指针或 iterator)▸ value:查找的值▸ lower_bound 查找的范围是 [first, last),返回的是序列中第⼀个大于等于 value 的元素的位置,时间为 O(logn)▸ [first, last)范围内的序列必须是提前排好序的,不然会错▸ 如果序列内所有元素都⽐ value ⼩,则返回last upper_bound(first, last, value) ▸ upper_bound 与 lower_bound 相似,唯⼀不同的地⽅在于upper_bound 查找的是序列中第⼀个⼤于 value 的元素 int main() { int arr[]={3,2,5,1,4}; sort(arr,arr+5);//需要先排序 cout << *lower_bound(arr,arr+5,3);//输出数组中第一个大于等于3的值 return 0; } unique() 去重函数(unique) unique(first, last): ▸ [first, last)范围内的值必须是一开始就提前排好序的▸ 移除 [first, last) 内连续重复项▸ 返回值:去重之后的返回最后一个元素的下一个地址(迭代器) #include using namespace std; int main() { int arr[]={3,2,2,1,2},n; sort(arr,arr+5);//需要先排序 n=unique(arr,arr+5)-arr;//n是去重后的元素个数 return 0; } reverse() 可反转容器等 string str="hello world , hi"; reverse(str.begin(),str.end());//str结果为 ih , dlrow olleh vector v = {5,4,3,2,1}; reverse(v.begin(),v.end());//容器v的值变为1,2,3,4,5 max() min() swap() 交换指针,可用于容器
简易版struct
#define pii pair //#define x first //#define y second using pii = pair; typdef pair pii; int main(){ pii a (1, 2); pii b = make_pair(3, 4); pair c ("name", make_pair(1, 2)); auto d = c; return 0; }
//建立方法: sets; multisets; unorded_sets; unorded_multisets; //如果Type无法进行比较,还需要和优先队列一样定义<运算符 //遍历方法: for(auto i:s)cout< 查找元素 sets; if(s.find(666) == s.end()){ cout << "666 was not in set"; } else{ cout << *(s.find(666)); } 注意 int main(){ set s; auto i = s.begin(); i++, i++, i++, i++; //i += 4; 错误 cout << *i; return 0; } 重载比较 set 容器模版需要3个泛型参数,如下:template class set; 第一个T 是元素类型,必选; 第二个C 指定元素比较方式,缺省为 Less, 即使用 < 符号比较; 第三个A 指定空间分配对象,一般使用默认类型。 因此: (1) 如果第2个泛型参数你使用默认值的话,你的自定义元素类型需要重载 < 运算操作; (2)如果你第2个泛型参数不使用默认值的话,则比较对象必须具有 () 操作,即: bool operator()(const T &a, const T &b) #include #include #include using namespace std; int m,k; struct cmp{ bool operator() (int a,int b){ if(abs(a-b)<=k){ return false; } else{ return a s; char op[10]; int x; int main(void){ scanf("%d%d",&m,&k); while(m--){ scanf("%s%d",op,&x); if(op[0]=='a'){ if(s.find(x)==s.end()){ s.insert(x); } } else if(op[0]=='d'){ s.erase(x); } else{ if(s.find(x)!=s.end()){ puts("Yes"); } else{ puts("No"); } } } return 0; } algorithm sort() sort(first, last, compare) first:排序起始位置(指针或 iterator)last:排序终⽌位置(指针或 iterator)compare:⽐较⽅式,可以省略,省略时默认按升序排序,如果排序的元素没有定义比较运算(如结构体),必须有comparesort 排序的范围是 [first, last),时间为 O(nlogn)作用:使指定容器范围内的元素有序,默认从小到大排序。可以排序所有已经定义的数据类型 cmp() 对于未定义 < 小于号的 数据类型,可以写一个cmp函数来定义排序的规则。 同样的,基本数据类型也可以通过cmp来自定义排序规则。 可更改升降序 #include #include #include #include using namespace std; const int n = 10; struct st{ int A_score; int B_score; }; st stu[n+5]; void putt();//输出stu //降序 bool cmp(st a,st b){//两个关键词的排序 if(a.A_score != b.A_score) return a.A_score > b.A_score; return a.B_score > b.B_score; } //升序 bool cmp(st a,st b){//两个关键词的排序 if(a.A_score != b.A_score) return a.A_score < b.A_score; return a.B_score < b.B_score; } int main() { srand(time(NULL)); for(int i = 0;i < n;i++){ stu[i].A_score = rand()%5 + 1; stu[i].B_score = rand()%5 + 1; } cout<<"排序前:"< 重载< 重载结构体的排序规则 #include #include #include #include using namespace std; const int n = 10; struct st{ int A_score; int B_score; bool operator < (const st b){ if(this->A_score != b.A_score) return this->A_score < b.A_score; return this->B_score < b.B_score; } }; st stu[n+5]; void putt();//输出stu int main() { srand(time(NULL)); for(int i = 0;i < n;i++){ stu[i].A_score = rand()%5 + 1; stu[i].B_score = rand()%5 + 1; } cout<<"排序前:"< next_permutation() 作用:用于求序列[first,last)元素全排列中一个排序的下一个排序 #include using namespace std; int main() { int a[3] = {0, 1, 2}; do { for (int i = 0; i < 3; i++) cout< 返回值:如果有下一个排列 则返回1,否则返回0。 二分函数 lower_bound(first, last, value) ▸ first:查找起始位置(指针或 iterator)▸ last:查找终⽌位置(指针或 iterator)▸ value:查找的值▸ lower_bound 查找的范围是 [first, last),返回的是序列中第⼀个大于等于 value 的元素的位置,时间为 O(logn)▸ [first, last)范围内的序列必须是提前排好序的,不然会错▸ 如果序列内所有元素都⽐ value ⼩,则返回last upper_bound(first, last, value) ▸ upper_bound 与 lower_bound 相似,唯⼀不同的地⽅在于upper_bound 查找的是序列中第⼀个⼤于 value 的元素 int main() { int arr[]={3,2,5,1,4}; sort(arr,arr+5);//需要先排序 cout << *lower_bound(arr,arr+5,3);//输出数组中第一个大于等于3的值 return 0; } unique() 去重函数(unique) unique(first, last): ▸ [first, last)范围内的值必须是一开始就提前排好序的▸ 移除 [first, last) 内连续重复项▸ 返回值:去重之后的返回最后一个元素的下一个地址(迭代器) #include using namespace std; int main() { int arr[]={3,2,2,1,2},n; sort(arr,arr+5);//需要先排序 n=unique(arr,arr+5)-arr;//n是去重后的元素个数 return 0; } reverse() 可反转容器等 string str="hello world , hi"; reverse(str.begin(),str.end());//str结果为 ih , dlrow olleh vector v = {5,4,3,2,1}; reverse(v.begin(),v.end());//容器v的值变为1,2,3,4,5 max() min() swap() 交换指针,可用于容器
sets; if(s.find(666) == s.end()){ cout << "666 was not in set"; } else{ cout << *(s.find(666)); }
int main(){ set s; auto i = s.begin(); i++, i++, i++, i++; //i += 4; 错误 cout << *i; return 0; }
set 容器模版需要3个泛型参数,如下:template class set; 第一个T 是元素类型,必选; 第二个C 指定元素比较方式,缺省为 Less, 即使用 < 符号比较; 第三个A 指定空间分配对象,一般使用默认类型。 因此: (1) 如果第2个泛型参数你使用默认值的话,你的自定义元素类型需要重载 < 运算操作; (2)如果你第2个泛型参数不使用默认值的话,则比较对象必须具有 () 操作,即: bool operator()(const T &a, const T &b)
#include #include #include using namespace std; int m,k; struct cmp{ bool operator() (int a,int b){ if(abs(a-b)<=k){ return false; } else{ return a s; char op[10]; int x; int main(void){ scanf("%d%d",&m,&k); while(m--){ scanf("%s%d",op,&x); if(op[0]=='a'){ if(s.find(x)==s.end()){ s.insert(x); } } else if(op[0]=='d'){ s.erase(x); } else{ if(s.find(x)!=s.end()){ puts("Yes"); } else{ puts("No"); } } } return 0; }
sort(first, last, compare)
对于未定义 < 小于号的 数据类型,可以写一个cmp函数来定义排序的规则。
同样的,基本数据类型也可以通过cmp来自定义排序规则。
可更改升降序
#include #include #include #include using namespace std; const int n = 10; struct st{ int A_score; int B_score; }; st stu[n+5]; void putt();//输出stu //降序 bool cmp(st a,st b){//两个关键词的排序 if(a.A_score != b.A_score) return a.A_score > b.A_score; return a.B_score > b.B_score; } //升序 bool cmp(st a,st b){//两个关键词的排序 if(a.A_score != b.A_score) return a.A_score < b.A_score; return a.B_score < b.B_score; } int main() { srand(time(NULL)); for(int i = 0;i < n;i++){ stu[i].A_score = rand()%5 + 1; stu[i].B_score = rand()%5 + 1; } cout<<"排序前:"< 重载< 重载结构体的排序规则 #include #include #include #include using namespace std; const int n = 10; struct st{ int A_score; int B_score; bool operator < (const st b){ if(this->A_score != b.A_score) return this->A_score < b.A_score; return this->B_score < b.B_score; } }; st stu[n+5]; void putt();//输出stu int main() { srand(time(NULL)); for(int i = 0;i < n;i++){ stu[i].A_score = rand()%5 + 1; stu[i].B_score = rand()%5 + 1; } cout<<"排序前:"< next_permutation() 作用:用于求序列[first,last)元素全排列中一个排序的下一个排序 #include using namespace std; int main() { int a[3] = {0, 1, 2}; do { for (int i = 0; i < 3; i++) cout< 返回值:如果有下一个排列 则返回1,否则返回0。 二分函数 lower_bound(first, last, value) ▸ first:查找起始位置(指针或 iterator)▸ last:查找终⽌位置(指针或 iterator)▸ value:查找的值▸ lower_bound 查找的范围是 [first, last),返回的是序列中第⼀个大于等于 value 的元素的位置,时间为 O(logn)▸ [first, last)范围内的序列必须是提前排好序的,不然会错▸ 如果序列内所有元素都⽐ value ⼩,则返回last upper_bound(first, last, value) ▸ upper_bound 与 lower_bound 相似,唯⼀不同的地⽅在于upper_bound 查找的是序列中第⼀个⼤于 value 的元素 int main() { int arr[]={3,2,5,1,4}; sort(arr,arr+5);//需要先排序 cout << *lower_bound(arr,arr+5,3);//输出数组中第一个大于等于3的值 return 0; } unique() 去重函数(unique) unique(first, last): ▸ [first, last)范围内的值必须是一开始就提前排好序的▸ 移除 [first, last) 内连续重复项▸ 返回值:去重之后的返回最后一个元素的下一个地址(迭代器) #include using namespace std; int main() { int arr[]={3,2,2,1,2},n; sort(arr,arr+5);//需要先排序 n=unique(arr,arr+5)-arr;//n是去重后的元素个数 return 0; } reverse() 可反转容器等 string str="hello world , hi"; reverse(str.begin(),str.end());//str结果为 ih , dlrow olleh vector v = {5,4,3,2,1}; reverse(v.begin(),v.end());//容器v的值变为1,2,3,4,5 max() min() swap() 交换指针,可用于容器
重载结构体的排序规则
#include #include #include #include using namespace std; const int n = 10; struct st{ int A_score; int B_score; bool operator < (const st b){ if(this->A_score != b.A_score) return this->A_score < b.A_score; return this->B_score < b.B_score; } }; st stu[n+5]; void putt();//输出stu int main() { srand(time(NULL)); for(int i = 0;i < n;i++){ stu[i].A_score = rand()%5 + 1; stu[i].B_score = rand()%5 + 1; } cout<<"排序前:"< next_permutation() 作用:用于求序列[first,last)元素全排列中一个排序的下一个排序 #include using namespace std; int main() { int a[3] = {0, 1, 2}; do { for (int i = 0; i < 3; i++) cout< 返回值:如果有下一个排列 则返回1,否则返回0。 二分函数 lower_bound(first, last, value) ▸ first:查找起始位置(指针或 iterator)▸ last:查找终⽌位置(指针或 iterator)▸ value:查找的值▸ lower_bound 查找的范围是 [first, last),返回的是序列中第⼀个大于等于 value 的元素的位置,时间为 O(logn)▸ [first, last)范围内的序列必须是提前排好序的,不然会错▸ 如果序列内所有元素都⽐ value ⼩,则返回last upper_bound(first, last, value) ▸ upper_bound 与 lower_bound 相似,唯⼀不同的地⽅在于upper_bound 查找的是序列中第⼀个⼤于 value 的元素 int main() { int arr[]={3,2,5,1,4}; sort(arr,arr+5);//需要先排序 cout << *lower_bound(arr,arr+5,3);//输出数组中第一个大于等于3的值 return 0; } unique() 去重函数(unique) unique(first, last): ▸ [first, last)范围内的值必须是一开始就提前排好序的▸ 移除 [first, last) 内连续重复项▸ 返回值:去重之后的返回最后一个元素的下一个地址(迭代器) #include using namespace std; int main() { int arr[]={3,2,2,1,2},n; sort(arr,arr+5);//需要先排序 n=unique(arr,arr+5)-arr;//n是去重后的元素个数 return 0; } reverse() 可反转容器等 string str="hello world , hi"; reverse(str.begin(),str.end());//str结果为 ih , dlrow olleh vector v = {5,4,3,2,1}; reverse(v.begin(),v.end());//容器v的值变为1,2,3,4,5 max() min() swap() 交换指针,可用于容器
作用:用于求序列[first,last)元素全排列中一个排序的下一个排序
#include using namespace std; int main() { int a[3] = {0, 1, 2}; do { for (int i = 0; i < 3; i++) cout< 返回值:如果有下一个排列 则返回1,否则返回0。 二分函数 lower_bound(first, last, value) ▸ first:查找起始位置(指针或 iterator)▸ last:查找终⽌位置(指针或 iterator)▸ value:查找的值▸ lower_bound 查找的范围是 [first, last),返回的是序列中第⼀个大于等于 value 的元素的位置,时间为 O(logn)▸ [first, last)范围内的序列必须是提前排好序的,不然会错▸ 如果序列内所有元素都⽐ value ⼩,则返回last upper_bound(first, last, value) ▸ upper_bound 与 lower_bound 相似,唯⼀不同的地⽅在于upper_bound 查找的是序列中第⼀个⼤于 value 的元素 int main() { int arr[]={3,2,5,1,4}; sort(arr,arr+5);//需要先排序 cout << *lower_bound(arr,arr+5,3);//输出数组中第一个大于等于3的值 return 0; } unique() 去重函数(unique) unique(first, last): ▸ [first, last)范围内的值必须是一开始就提前排好序的▸ 移除 [first, last) 内连续重复项▸ 返回值:去重之后的返回最后一个元素的下一个地址(迭代器) #include using namespace std; int main() { int arr[]={3,2,2,1,2},n; sort(arr,arr+5);//需要先排序 n=unique(arr,arr+5)-arr;//n是去重后的元素个数 return 0; } reverse() 可反转容器等 string str="hello world , hi"; reverse(str.begin(),str.end());//str结果为 ih , dlrow olleh vector v = {5,4,3,2,1}; reverse(v.begin(),v.end());//容器v的值变为1,2,3,4,5 max() min() swap() 交换指针,可用于容器
返回值:如果有下一个排列 则返回1,否则返回0。
lower_bound(first, last, value)
upper_bound(first, last, value)
int main() { int arr[]={3,2,5,1,4}; sort(arr,arr+5);//需要先排序 cout << *lower_bound(arr,arr+5,3);//输出数组中第一个大于等于3的值 return 0; }
去重函数(unique)
unique(first, last):
#include using namespace std; int main() { int arr[]={3,2,2,1,2},n; sort(arr,arr+5);//需要先排序 n=unique(arr,arr+5)-arr;//n是去重后的元素个数 return 0; }
可反转容器等
string str="hello world , hi"; reverse(str.begin(),str.end());//str结果为 ih , dlrow olleh vector v = {5,4,3,2,1}; reverse(v.begin(),v.end());//容器v的值变为1,2,3,4,5
交换指针,可用于容器
上一篇 cgb2108-day07
下一篇 数据库系统概论第三章课后习题第四题数据库表及习题答案
版权所有 (c)2021-2022 MSHXW.COM
ICP备案号:晋ICP备2021003244-6号