好像C++的STL默认的排序都是升序(除了priority_queue)
multiset使用总结
multiset就是一颗平衡二叉树,支持log插入,log查询,log删除,还有一个重点就是是能进行二分操作。
multiset<int> S;//声明 S.insert(val);//时间复杂度为logn S.insert(pos,val);//指定位置插入,可以提高插入速度 S.insert(S.end(),val);//常数是S.insert(val)的1/3 S.insert(S.begin(),val);//常数和S.insert(val)一样 auto pos=S.lower_bound(val);//时间复杂度为logn,常数是insert的1/3,返回第一个大于等于val的迭代器指针,如查找失败则返回S.end(); auto pos=lower_bound(S.begin(),S.end(),val);//千万不能用,时间复杂度是O(n)的!!!!!! auto pos=S.find(val);//时间复杂度为logn,常数是insert的1/3,返回值等于val的迭代器指针,如查找失败则返回S.end(); S.erase(pos)//删除pos迭代器的值,时间复杂度为logn,常数是insert的1/3 S.erase(val)//删除所有等于val的值,若有m个数等于val,则时间复杂度为mlogn,常数是insert的1/3 S.erase(begin,end)//删除begin和end区间,时间复杂度同上。总之删除单个元素时间复杂度为logn S.clear();//同erase,对单个元素操作也是logn,删除所有元素为nlogn。常数较小
tuple使用总结
配合set进行多元组去重特别好用
tuple可以看作是pair的扩充
有时候使用tuple代替结构体存储多元组省去很多麻烦。
tuple可以比较大小(按字典序比较)
注意与tie函数的搭配
tuple<int,int,int,int> T; int main(void) { int a,b,c,d; tie(a,b,c,d)=make_tuple(1,2,3,4); //c++11 用make_tuple生成一个tuple T=make_tuple(5,6,7,8); //T={5,6,7,8}; c++17才允许使用这种方法,所以竞赛肯定就不能用了 printf("%d\n", c); //tuple访问数据元素的第一种方式,得配合tie函数 printf("%d\n", get<0>(T)); // tuple访问数据元素的第二种方式 //auto [w,x,y,z]=T; tuple访问数据元素的第三种方式,不过只支持c++17 //printf("%d\n", y); }
complex使用总结
complex有时候可以作point使用
typedef complex<double> CP; int main(void) { CP a(1,0); CP b(2,3); cout<<(a+b)<<endl; cout<<(a-b)<<endl; cout<<(a*b)<<endl; cout<<(a/b)<<endl; cout<<abs(a)<<endl;//取模长 cout<<norm(a)<<endl;//取模长的平方,范数 }
map的使用
#include<bits/stdc++.h> using namespace std; map<string,int> M; int main(void) { M["hello"]=100; M["world"]=1000; auto it=M.find("12"); printf("%d %d\n", it==M.end(),M.size()); for(auto it=M.begin();it!=M.end();it++) //前向遍历 printf("M: %d\n",it->second); for(auto it=M.rbegin();it!=M.rend();it++)//后向遍历 printf("M: %d\n",it->second); } /* 输出: 1 2 M: 100 M: 1000 M: 1000 M: 100 */
string
#include<bits/stdc++.h> using namespace std; string a; string b; int main(void) { a="aaa"; b="ccc"; printf("%d %d %d\n", a==b,a>b,a<b); a="cc"; printf("%d %d %d\n", a==b,a>b,a<b); a="ccca"; printf("%d %d %d\n", a==b,a>b,a<b); a="cd"; printf("%d %d %d\n", a==b,a>b,a<b); string c; c=a+b; cout<<c<<" "<<c.length()<<" "<<c[1]<<endl; string d="bnubeginner"; printf("%d %d\n", d.find("be"),d.find("bei")); d.insert(3,"xyz"); cout<<d<<endl; d.erase(3,3);//第一个参数是起始位置,第二个参数是长度 cout<<d<<endl; string e; e.assign(d,0,3); cout<<e<<endl; } /* 0 0 1 0 0 1 0 1 0 0 1 0 cdccc 5 d 3 -1 bnuxyzbeginner bnubeginner bnu */