我是靠谱客的博主 知性月光,这篇文章主要介绍C++刷题小知识之容器,有序集合,映射小结,现在分享给大家,希望可以做个参考。

对容器vector,set,map这些遍历的时候都是用迭代器访问
可以将定义迭代器
vector::itetator 等价于 auto

复制代码
1
2
3
4
5
6
7
vector<int > v(10,2); for(auto *it = v.begin();it != v.emd();it++) { cout<<*it<<" "; }

C++STL之集合set的使用:
set是一个集合,一个set里面的各个元素是各不相同的,而且set会按照元素进行从小到大的排序

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**************** * set自动排序 * set自动清除重复值 *****************/ #include <iostream> #include <set> using namespace std; int main() { set<int> s; s.insert(1); cout<<*(s.begin())<<endl; for(int i=6;i>=0;i--) { s.insert(i); } for(auto it=s.begin();it != s.end();it++) cout<<*it<<" ";//输出 0 1 2 3 4 5 6 cout<<endl; //能找到2,所以返回 1 cout<<(s.find(2)!= s.end())<<endl; //不能找到10,所以返回 0 cout<<(s.find(10)!= s.end())<<endl; s.erase(1); //不能找到1,所以返回 0 cout<<(s.find(1) != s.end()) <<endl; return 0; }

C++STL之映射map的使用:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/**************** * map<key,value> * key: 键 * value: 值 * 会根据"值"自动排序 *想输出 key 用m->first *想输出 value 用m->value *****************/ #include <iostream> #include <map> using namespace std; int main() { map<string , int> m; m["hello"] = 2; cout<<m["hello"] << endl; //输出 2 cout<<m["world"] << endl; //print 0 m["world"] = 3; m[","] = 1; //traverse /* 输出: , 1 hello 2 world 3 */ for(auto it = m.begin();it != m.end();it++) cout<<it->first<<" "<<it->second<<endl; /* print the first elem. it's key and value */ cout<<m.begin()->first<<" "<<m.begin()->second<<endl; /* print the last elem. it's key and value */ cout<<m.rbegin()->first<<" "<<m.rbegin()->second<<endl; cout<<m.size()<<endl;

C++STL 之队列:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream> #include <queue> using namespace std; int main() { queue<int> q; for(int i = 0; i< 6;i++) { q.push(i); //enqueue 0 1 2 3 4 5 } cout<< q.front() << " "<<q.back()<<endl; //print : 0 5 cout<< q.size() <<endl; //print : 6 q.pop(); //dequeue : 0 cout<< q.front(); //print :1 return 0; }

C++STL之bitset

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream> #include <bitset> using namespace std; int main() { unsigned int u =1000; bitset<5> b(u); //初始化方式 //bitset<5> b; 都为0 //bitset<5> b(u); u为unsigned int 如果 u=1,则初始化为10000 //bitset<5> b(s); s为字符串,如“1101” -> "10110" //bitset<5> b(s.pos,n);从字符串的s[pos]开始,n位长度 for(int i=0;i<5;i++) cout<<b[i]; cout<<endl<<b.any();//b中是否存在1的二进制位为1 cout<<endl<<b.none();//b中不存在1? cout<<endl<<b.count();//b中1的二进制位的个数 cout<<endl<<b.size();//b中二进制位的个数 cout<<endl<<b.test(1);//测试下标为2是否为1 b.set(4);//把小标为4的置为1 b:11001 b.reset();//所有位归零 b:00000 b.flip();//所有位取反 b:11111 b.reset(3); //b的下标3处归零b:11101 return 0; }

cctype的使用
中已经定义了字符所属的范围
isalpha(大写+小写字母),islower(小写字母),isupper(大写字母),isalnum(字母大写和数字),isblank(space和t)。isspace(space,t,r,n)
C++11新特性:
①for循环与auto

复制代码
1
2
3
4
5
6
forint i-0;i<v.size();i++) cout<<v[i]; 等价于 forauto i :v) cout<<i;

②to_string
to_string();把int型转化为字符串

③stoi,stod
字符串转整型和double

最后

以上就是知性月光最近收集整理的关于C++刷题小知识之容器,有序集合,映射小结的全部内容,更多相关C++刷题小知识之容器内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(62)

评论列表共有 0 条评论

立即
投稿
返回
顶部