Associative Containers
来源:互联网 发布:淘宝店铺开店流程 编辑:程序博客网 时间:2024/05/18 01:44
Associative Containers
mapmultimapsetmultiset
1.map
关联数组,可以利用一个元素索引出另外一个
先来段代码
map<string, size_t> word_count; //新建一个mapstring word;while(cin >> word){++word_count[word];}for(const auto &w : word_count){cout << w.first << " occurs" << w.second << ((w.second > 1) ? " times" : " time") << endl;}
*本来word_count[word]并不存在,新建一个,然后将这个的value值置为1,如果存在直接+1
*在map中存储的是pair类型,定义在utility头文件中。
成员变量
key_type mapped_type value_type//这个是pair类型的
操作
增加
两种
insert
string word;word-count.insert({word, 1});word_count.insert(make_pair(word, 1));word_count.insert(pair<string, size_t>(word, 1)); //推荐,前两个都需要c++11支持word_count.insert(map<string, size_t>::value_type(word, 1));
返回值是pair类型 , first 是这个元素的iterator,second是bool类型,表示这个元素是否被正确插入。
[]
见第一个程序片段
删
erase()
这个方法有3个重载,参数分别是索引值,iterator和iterator的范围
参数是iterator, 返回值是iterator,被删元素的下一个。
参数是key_vaule, 返回值是size_type, 被删了几个。
取值
两种
[]
multimap无法取值,因为取出来的值不只有一个
可以用[]取值,但是这样不好,因为万一没有这个值,会自动新建一个,见代码1
find
if(word_count.find("foobar") == word_count.end()) cout << "foobar is not in the map" << endl;
2. set
一句话:就是个集合
先来段代码
// define a vector with 20 elements, // holding two copies of each number from 0 to 9 vector<int> ivec; for (vector<int>::size_type i = 0; i != 10; ++i) { ivec.push_back(i); ivec.push_back(i); // duplicate copies of each number } // iset holds unique elements from ivec; miset holds all 20 elements set<int> iset(ivec.cbegin(), ivec.cend()); multiset<int> miset(ivec.cbegin(), ivec.cend()); cout << ivec.size() << endl; // prints 20 cout << iset.size() << endl; // prints 10 cout << miset.size() << endl; // prints 20 // returns an iterator that refers to the element with key == 1 iset.find(1); iset.find(11); // returns the iterator == iset.end() iset.count(1); // returns 1 iset.count(11); // returns 0 // returns an iterator to the first element with key == 1 iset.find(1); iset.find(11); // returns the iterator == iset.end() miset.count(1); // returns 2 miset.count(11); // returns 0 set<string> set1; // empty set set1.insert("the"); // set1 now has one element set1.insert("and"); // set1 now has two elements#ifdef LIST_INIT ivec = {2,4,6,8,2,4,6,8}; // ivec now has eight elements set<int> set2; // empty set set2.insert(ivec.cbegin(), ivec.cend()); // set2 has four elements set2.insert({1,3,5,7,1,3,5,7}); // set2 now has eight elements#else set<int> set2; // empty set int temp[] = {2,4,6,8,2,4,6,8}; set2.insert(begin(temp), end(temp)); int temp2[] = {1,3,5,7,1,3,5,7}; set2.insert(begin(temp2), end(temp2));#endif
不知道set到底有什么用,知道了再说吧
0 0
- Associative Containers
- !!!Chapter 10 Associative Containers
- Chapter 11.Associative Containers
- STL associative containers(关联容器)
- C++ unordered Associative Containers(C++11)
- 五 关联式容器 associative containers
- 《C++ Primer第五版》读书笔记(9)--ASSOCIATIVE CONTAINERS
- associative
- STL Associative Containers Types:map、set、multimap、mutilset以及对应的unordered类型
- Effective STL 20 Specify comparison types for associative containers of pointers
- Containers
- Associative References
- Associative(关联)
- 关联(Associative)
- Sequentials Containers
- sequentail Containers
- Understanding Containers
- STL Containers
- OC_NSNumer和NSValue
- jquery的几种ajax方式
- c++ 迭代器与下标访问效率问题
- bootstrap(组件)
- 任务管理(3.3)------嵌入式系统软件及操作系统知识
- Associative Containers
- 在python的应用程序中,不需要关系型数据库时,如MySQL可以使用python提供的持久字典dbm来存储名称和值(键值对)
- 遥感在水环境监测中的应用
- android新版本下获取屏幕宽度和高度的方法
- 初学弹窗
- poj 3620 求最大连通水池的个数(深度优先搜索)
- 在使用Android-PullToRefresh前遇到的问题
- Notification讲解
- stm32 对GPIO口操作后 寄存器复位情况