STL by pcccc
来源:互联网 发布:长板女生知乎 编辑:程序博客网 时间:2024/05/29 09:35
前言
所有常用操作都在代码里
STL可能会很慢,时间允许的话可替代操作还是手写好
建议对每一种都盲敲2~3遍代码,不要眼高手低
Vector
#include<iostream>#include<cstdio>#include<vector>//!!!头文件using namespace std;vector<int> a[100];int main(){ for(int i=1;i<=200;i++) a[1].push_back(i);//插入 a[1].pop_back();//弹出 cout<<a[1].size()<<endl;//大小 for(int i=0;i<199;i++) cout<<a[1][i]<<endl;}
Map
Map是c++的一个标准容器,按字面意思就是地图的意思
即给你一个元素,根据“地图”找到这个元素对应的元素
/*头文件<map>*/#include <map>#include <iostream>using namespace std;int main(){ map<string,int> a; cout<<"插入:"<<endl; a["April"]=112;//插入 cout<<a["April"]<<endl; cout<<endl; cout<<"查询:"<<endl; map<string,int>::iterator pc;//查询一个元素是否在map里 pc=a.find("April"); if(pc==a.end()) cout<<"Not Find"<<endl;//如果找不到会自动返回“指向map尾部的迭代器” else cout<<pc->second<<endl;//输出"April"对应的键值 cout<<endl; cout<<"遍历"<<endl; a["June"]=1;//遍历 a["July"]=2; map<string,int>::iterator i; for(i=a.begin(); i!=a.end(); i++)//不能等于a.end() { cout<<i->second<<endl; } cout<<endl; cout<<"判断map是否是空集"<<endl; cout<<a.empty()<<endl;//判断map是否是空集 cout<<endl; cout<<"交换两个map的元素 "<<endl; map<int ,int> b;//交换两个map的元素 map<int ,int> c; c[1]=1; c[2]=2; b[1]=1000; swap(b,c); map<int,int> ::iterator j; for(j=b.begin();j!=b.end();j++) { cout<<j->second<<endl; } for(j=c.begin();j!=c.end();j++) { cout<<j->second<<endl; }}
- map中 swap的用法:
Map中的swap不是一个容器中的元素交换,而是两个容器交换; - Map中的元素是自动按key升序排序
#include <map> #include <iostream> using namespace std; int main( ) { map <int, int> m1; map <int, int>::iterator m1_Iter; m1[1]=20; m1[4]=40; m1[3]=60; m1[2]=50; m1[6]=40; m1[7]=30; cout << "The original map m1 is:"<<endl; for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ ) cout << m1_Iter->first<<" "<<m1_Iter->second<<endl;}/*The original map m1 is: 1 20 2 50 3 60 4 40 6 40 7 30*/
- map的基本操作函数:
begin() 返回指向map头部的迭代器 clear() 删除所有元素 count() 返回指定元素出现的次数 empty() 如果map为空则返回true end() 返回指向map末尾的迭代器 *equal_range() 返回特殊条目的迭代器对 erase() 删除一个元素 find() 查找一个元素 get_allocator() 返回map的配置器 insert() 插入元素 key_comp() 返回比较元素key的函数 lower_bound() 返回键值>=给定元素的第一个位置 max_size() 返回可以容纳的最大元素个数 rbegin() 返回一个指向map尾部的逆向迭代器 rend() 返回一个指向map头部的逆向迭代器 size() 返回map中元素的个数 swap() 交换两个map upper_bound() 返回键值>给定元素的第一个位置 value_comp() 返回比较元素value的函数
Set
- set作为一个容器也是用来存储同一数据类型的数据类型,并且能从一个数据集合中取出数据,在set中每个元素的值都唯一,而且系统能根据元素的值自动进行排序。应该注意的是set中数元素的值不能直接被改变。
- set中常用的方法
begin() 返回set容器的第一个元素end() 返回set容器的最后一个元素clear() 删除set容器中的所有的元素empty() 判断set容器是否为空size() 返回当前set容器中的元素个数count() 用来查找set中某个某个键值出现的次数。这个函数在set并不是很实用,因为一个键值在set只可能出现0或1次,这样就变成了判断某一键值是否在set出现过了。find() ,返回给定值值得定位器,如果没找到则返回end()。
#include <iostream>#include <set>using namespace std;int main(){ set<int> s; s.insert(1); s.insert(2); s.insert(3); s.insert(1); cout<<"set 的 size 值为 :"<<s.size()<<endl; cout<<"set 的 maxsize的值为 :"<<s.max_size()<<endl; cout<<"set 中的第一个元素是 :"<<*s.begin()<<endl; cout<<"set 中的最后一个元素是:"<<*s.end()<<endl; s.clear(); if(s.empty()) { cout<<"set 为空 !!!"<<endl; } cout<<"set 的 size 值为 :"<<s.size()<<endl; cout<<"set 的 maxsize的值为 :"<<s.max_size()<<endl; return 0;}
- lower_bound(key_value) ,返回第一个大于等于key_value的定位器
- upper_bound(key_value),返回最后一个大于等于key_value的定位器
#include <iostream>#include <set>using namespace std;int main(){ set<int> s; s.insert(1); s.insert(3); s.insert(4); cout<<*s.lower_bound(2)<<endl; cout<<*s.lower_bound(3)<<endl; cout<<*s.upper_bound(3)<<endl; return 0;}
useful link about set
Queue
普通队列大家都会写吧
我们来看看如何用queue来实现堆的功能
//小根堆 #include<iostream>#include<cstdio>#include<queue>using namespace std;priority_queue<int,vector<int>,greater<int> >q;//默认是大根堆,小根堆需要自己加greater<int>int main(){ q.push(1); q.push(2); q.push(3); while(q.size()) { cout<<q.top()<<endl; q.pop(); }}
//大根堆 #include<iostream>#include<cstdio>#include<queue>using namespace std;priority_queue<int>q;int main(){ q.push(1); q.push(2); q.push(3); while(q.size()) { cout<<q.top()<<endl; q.pop(); }}
Bitset
bitset可以看作bool数组,但优化了空间复杂度和时间复杂度,并且可以像整型一样按位与或。
#include<iostream>#include<cstdio>#include<bitset>using namespace std;bitset<10> a;//<>中间是长度!!!bitset<10> b;int main(){ a.set();//全部置为1 cout<<a<<endl; a.reset();//全部置为0 a[1]=1;//可以直接把它当作一个数来进行二进制操作 a=a|b; cout<<a.count()<<endl; //输出bitset里有多少个1 cout<<a<<endl; a.flip();//将bitset里的元素翻转一下 cout<<a;}
- <>中间是长度!!!
- bitset空间占用十分小,运行速度也很快!!
奇技淫巧 - 常用的成员函数:
b.any() b中是否存在置为1的二进制位?b.none() b存在置为1的二进制位吗?b.count() b中置为1的二进制位的个数b.size() b中二进制位数的个数b[pos] 访问b中在pos处二进制位b.test(pos) b中在pos处的二进制位置为1么?b.set() 把b中所有二进制位都置为1b.set(pos) 把b中在pos处的二进制位置为1b.reset( ) 把b中所有二进制位都置为0b.reset( pos ) 把b中在pos处的二进制位置置为0b.flip( ) 把b中所有二进制位逐位取反b.flip( pos ) 把b中在pos处的二进制位取反b.to_ulong( ) 把b中同样的二进制位返回一个unsignedb._Find_next(pos) 返回bitset在位置pos之后第一个1的位置
useful link about Bitset
lower_bound&upper_bound
函数lower_bound()在first和last中的前闭后开区间进行二分查找,返回大于或等于**val的第一个元素位置。如果所有元素都小于val,则返回last的位置
用法: int t=lower_bound(a+l,a+r,m)-a函数upper_bound()在first和last中的前闭后开区间进行二分查找,返回大于val的第一个元素位置。如果所有元素都小于val,则返回last的位置
复杂度O(logn)
阅读全文
0 0
- STL by pcccc
- split STL string by chars
- Practical Guide to STL By Jeff Bogan
- STL中的排序算法一览[By ACM郭老师]
- Effective STL 29 istreambuf_iterators for character-by-character input
- 深入研究 STL Deque 容器-An In-Depth Study of the STL Deque Container (By Nitron)
- by
- 《关于STL中的list,vector,deque队列容器(written by Flower)》
- stl
- stl
- STL
- STL
- STL
- STL
- STL
- STL
- STL
- stl
- Alamofire 网络请求报错 Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed
- 抢先体验Kindle Paperwhite 3 固件 5.9.2 及 手动升级 Kindle固件教程
- 数据库 Mysql
- Laravel 全文检索 Scout集成Algolia
- Java消息中间件学习九 -- ActiveMQ与HA架构(master/slave)
- STL by pcccc
- 二叉树面试题汇总(一)
- 用于分类/特征提取的CNN结构设计方法总结
- PyQt笔记005——pyqt5去掉标题栏
- 【XSY2612】Comb Avoiding Trees 生成函数 多项式求逆 矩阵快速幂
- PSO Optimal Design Package (PODPack)
- linux交叉编译VLC的webPlugin--npapi-vlc/npapi-vlc的交叉编译
- win 10系统安装jdk 64位
- Spring详细教程