C++Primer_第11章_关联容器

关联容器和顺序容器有着根本的不同。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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

luuyiran

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值