C++Primer_第11章_关联容器
来源:互联网 发布:mac电池健康度怎么看 编辑:程序博客网 时间:2024/05/17 10:25
关联容器和顺序容器有着根本的不同。Associative and sequential containers differ from one another in a fundamental way:Elements in an associative container are stored and retrieved by a key. In contrast,elements in a sequential container are stored and accessed sequentially by their position in the container.
retrieve data 检索数据 retrieve 取回 检索 挽回
类型map和multimap定义在头文件map中;
set和multiset定义在头文件set中;
无序容器则定义在头文件unordered_map和unordered_set中。
#include<iostream>#include<map>#include<set>#include<string>using std::map;using std::set;using std::string;using std::cin;using std::cout;int main(){map <string, size_t> word_count;set<string> exclude = { "The","But","And","Or","An","A","the","but","and","or","an","a" };string word;while (cin >> word)if(exclude.find(word)==exclude.end())++word_count[word];for (const auto &w : word_count)cout << w.first << " occurs(出现了)" << w.second << ((w.second > 1) ? " times" : " time") << std::endl;system("pause");return 0;}在统计每个单词出现次数之前,我们检查单词是否在忽略集合中,这是在if语句中完成的:
//只统计不在exclude中的单词if(exclude.find(word)==exclude.end())find调用返回一个迭代器,如果给定关键字在set中,迭代器指向该关键字,否则,find返回尾后迭代器。
Exercise 11.1
Describe the differences between a map and a vector.
A map
is a collection of key-value pairs. we can get a value lookup by key efficiently.
A vector
is a collection of objects, and every object has an associated index, which gives access to that object.
Exercise 11.2
Give an example of when each of list, vector, deque, map, and set might be most useful.
list
: a to-do list. always need insert or delete the elements anywhere.vector
: save some important associated data, always need query elements by index.deque
: message handle. FIFO.map
: dictionary.set
: bad_checks.
Exercise 11.5
Explain the difference between a map and a set. When might you use one or the other?
set
: the element type is the key type.map
: we should use a key-value pair, such as{key, value}
to indicate that the items together from one element in the map.
I use set
when i just need to store the key
, In other hand, I would like use map
when i need to store a key-value pair.
Exercise 11.6
Explain the difference between a set and a list. When might you use one or the other?
set
is unique and order, but list
is neither. using which one depend on whether the elements are unique and order to store.
练习11.26
#include <iostream>#include <map>#include <string>#include <algorithm>#include <vector>int main(){std::map<int, int> m;m[0] = 1;for (const auto& e : m) std::cout << e.first << " " << e.second << "\n";std::map<int, std::string> map = { { 1, "ss" },{ 2, "sz" } };for (const auto& e : map) std::cout << e.first << " " << e.second << "\n";std::map<int, std::string>::key_type type_to_subscript = 1;std::map<int, std::string>::mapped_type type_to_return =map[type_to_subscript];std::cout << type_to_subscript <<" "<< type_to_return<<"\n";system("pause");return 0;}//输出// 0 1// 1 ss// 2 sz// 1 ss
- C++Primer_第11章_关联容器
- C++Primer_第7章_类
- C++primer_第九章_顺序容器_学习跟踪
- C++primer_关联容器之map
- C++Primer_第13章_拷贝控制
- C++primer_第四章_表达式_学习跟踪
- C++primer_第五章_语句_学习跟踪
- C++primer_第六章_函数_学习跟踪
- C++primer_第七章_类_学习跟踪
- 第11章-关联容器
- C++primer_第八章_IO库_学习跟踪
- 重新学习《C++Primer5》第11章-关联容器
- 《C++Primer》读书笔记——第11章 关联容器
- C++primer_顺序容器之string操作
- C++Primer_笔记_异常处理
- C++Primer第10章关联容器习题
- 【C++primer学习笔记】第10章 关联容器
- C++primer第4版第十章关联容器
- uiautomator中如何判断开关wifi开启和关闭是否正确
- CSS缩写
- pyqt Qtableview,duplicate connection name 'qt_sql_default_connection', old connection removed.
- EF_85mm_1.2L_II_USM拆机电路板靓图
- window.location (小结)
- C++Primer_第11章_关联容器
- CreateThread导致内存泄露的原因
- 强删文件--->构建IRP---->独占--->正在运行 以及磁盘读写(思路)
- [caioj 1425][状态压缩]游戏---状态压缩dp
- A
- 数字图像处理与OpenCV总结篇:一
- ubuntu 永久修改主机名
- DI之循环依赖 ——spring
- 用python实现五大基本算法