C++STL小记

张开发
2026/4/10 20:25:22 15 分钟阅读

分享文章

C++STL小记
动态数组vector#includevector构造二维数组vectorvectorint vct(100,vectorint()); //100行 列数未定 vectorvectorint vct(22,vectorint(33,-1)) //22行33列 初值-1常用方法push_back() pop_back()size() empty() clear()改变长度 resize()//原本数组1 2 3 q.resize(5);//1 2 3 0 0默认增加的值为0 q.resize(5,3);//1 2 3 3 3 q.resize(2);//1 2注意事项.size()返回值为size_t具体范围根据编译器改变——注意小心size()溢出栈stack#includestack常用方法push() pop()top()size() empty()——无clear()注意事项不可用下标访问 stk[i]队列queue#includequeue常用方法push() pop()front() back()取队首 取队尾size() empty()——无clear()注意事项不可用下标访问 que[i]双端队列deque#includedeque常用方法push_front() pop_front() push_back() pop_back()访问头/尾 front() back()size() empty()clear()可以用[i]访问内部元素二叉堆priority_queue#includepriority_queue构造priority_queueint pque1; // 储存int的大顶堆 priority_queueint, vectorint, greaterint pque2; // 储存int的小顶堆常用方法push() pop()top()size() empty()——无clear()注意事项不可用下标访问 que[i]集合set#includeset #includeunordered_setset/multiset/unordered_set区别setmultisetunordered_set一个元素只能在集合中出现一次只出现一次可出现多次只出现一次集合中元素有顺序有顺序小到大有小到大无构造////set #includeset setint st1; // 储存int的集合从小到大 setint, greaterint st2; // 储存int的集合从大到小 ////multiset #includeset multisetint mts1; // 储存int的集合从小到大 multisetint, greaterint mts2; // 储存int的集合从大到小 ////unordered_set #includeunordered_set unordered_setint ust1;//无序 只这一种构造方式遍历for (auto ele : st) cout ele endl;常用方法插入元素insert() 删除元素erase()查找元素find() 判断是否存在count()size() empty() clear()注意事项不可用下标访问 que[i]元素只读不可用下标定位修改映射map#includemap #includeunordered_setmap/multimap/unordered_map区别mapmultimapunordered_map一个键只能在映射中出现一次只出现一次可出现多次只出现一次集合中元素有顺序有顺序小到大有小到大无构造////map #include map mapint, int mp1; // int-int按键从小到大 mapint, int, greaterint mp2; // int-int按键从大到小 mapstring, vectorint mp3; // string-vector mp1.insert({2, 1}); //赋值 mp1[2]1; //赋值 ////multimap #include map multimapint, int mmp1; // int-int按键从小到大 multimapint, int, greaterint mmp2; // int-int按键从大到小 multimapstring, vectorint mmp3; // string-vector mmp1.insert({2, 1}); //赋值 不能用mmp1[2]1; ////unordered_map unordered_mapint, int ump1; unordered_mapstring, vectorint ump2;遍历.first .secondfor (auto pr : mp) cout pr.first pr.second endl;常用方法插入元素insert() 删除元素erase()查找元素find() 判断是否存在count()size() empty() clear()注意事项map/unordered_map可用下标访问 mp[1]——如果有对应值输出 如果没有会帮你插入这个键值对其中值默认为0mapchar, int mp; cout mp.count(a) endl; // 0 mp[a]; // 即使什么都没做此时mp[a]0已经插入了 cout mp.count(a) endl; // 1 cout mp[a] endl; // 0字符串string#includestring构造string s1; // 构造字符串为空 string s2 awa!; // 构造字符串并赋值awa! string s3(10, 6); // 构造字符串通过构造函数构造为6666666666常用方法连接 尾接取子串 substr(a,b)——a是起始值(0开始)b是字串长度查找字符串 find(“子串”)——返回子串的起始下标如果不存在返回npos值string s1abcabcd; cout s1.substr(3,3); //abc cout s1.find(bca); //1 if (s1.find(bbb) ! npos) cout YES endl; //验证s1中不存在bbb子串数值与字符串互转C11源目的函数int / long long / float / double / long doublestringto_string()stringintstoi()stringlong longstoll()stringfloatstof()stringdoublestod()stringlong doublestold()string s123; int xstoi(s); coutx; //123二元组pair#includeutility和unordered_map区别pair只能存一对firstsecond,unordered_map可以存很多对构造/方法pairint,int p1{1,2}; pairchar,int p1{a,1}; //取值 char xp2.first; int yp2.second; //判同 pairint, int p1 {1, 2}; pairint, int p2 {1, 3}; if (p1 p2) { ... } // false常用算法swap(a,b);//a b无论什么类型都可以sort()默认从小到大排序vectorint arr{1, 9, 1, 9, 8, 1, 0}; sort(arr.begin(), arr.end());//默认从小到大排序 arr [0, 1, 1, 1, 8, 9, 9] sort(arr.begin(), arr.end(), greaterint());// arr [9, 9, 8, 1, 1, 1, 0]特殊排序要手写比较器返回值true/false//希望pair里面第二个数从小到大排序如果第二个数相同则第一个数从大到小 bool cmp(pairint, int a, pairint, int b) { if (a.second ! b.second) return a.second b.second; return a.first b.first; } int main() { vectorpairint, int arr{{1, 9}, {2, 9}, {8, 1}, {0, 0}}; sort(arr.begin(), arr.end(), cmp); // arr [(0, 0), (8, 1), (2, 9), (1, 9)] }lower_bound() / upper_bound()lower_bound(): 寻找 x 的第一个元素的位置upper_bound(): 寻找 x 的第一个元素的位置怎么找 小于等于x 的第一个元素呢x 的第一个元素的前一个元素如果有便是 x 的第一个元素x 的第一个元素的前一个元素如果有便是 x 的第一个元素vectorint arr{0,1,1,1,8,8,9}; int pos lower_bound(arr.begin(), arr.end(), 8) - arr.begin(); //4 int pos upper_bound(arr.begin(), arr.end(), 8) - arr.begin(); //6 int pos lower_bound(arr.begin()4, arr.end(), 8) - arr.begin(); //5 ---4则从第四个开始找 int pos lower_bound(arr.begin(), arr.end(), 999) - arr.begin(); //7 找不到就返回尾迭代器reverse()vectorint arr{0,1,1,1,8,8,9}; reverse(arr.begin(), arr.end()); //9 8 8 1 1 1 0 reverse(arr.begin()2, arr.begin()5); //0 1 8 1 1 8 9 ----虽然是5但是是翻转到第四位就结束max() / min()unique()把重复元素挤到最后面只用unique时数组元素个数不变。unique返回的是重复元素的起始位置vectorint arr{1,1,4,3,6,6,8,4}; sort(arr,begin(), arr.end()); //1 1 3 4 4 6 6 8 cout unique(arr.begin(), arr.end()); //1 3 4 6 8 1 4 6 --返回6 arr.erase(unique(arr.begin(), arr.end()) , arr.end()); //1 3 4 6 8数学函数所有函数参数均支持int/long long/float/double/long doublepow(2, 1.0/3)round是四舍五入算特别大的数的时候根号/幂会不准gcd() / lcm()返回最大公约数/最小公约数int x gcd(8, 12); // 4 int y lcm(8, 12); // 24

更多文章