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 取回 检索 挽回


类型mapmultimap定义在头文件map中;

setmultiset定义在头文件set中;

无序容器则定义在头文件unordered_mapunordered_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.

map is a collection of key-value pairs. we can get a value lookup by key efficiently.

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