关联容器_map类型

1map对象的定义

//count number of times each word occurs int the input
map<string, int> word_count; //empty map from string to int

这段代码定义了一个名为word_count 的map对象,键索引为string类型,关联值为int型。即map为键——值对的集合。

//map的构造函数
map<k,v> m;    //创建一个map对象,名为m,键为k,值为v。
map<k,v> m(m2);    //创建m2的副本m,m与m2必须有相同的键类型和值类型。
map<k,v> m(b,e);    //创建一个map对象m,存储迭代器b和e标记的范围内所有元素的副本。元素的类型必须能转换为pair<const k,v>

 对于键类型,唯一的约束就是必须支持<操作符。

2map定义的类型

//map类定义的类型
map<K,V>::key_type    //在map容器中,用作索引的键的类型
map<K,V>::mapped_type    //在map容器中,键所关联的值的类型
map<K,V>::value_type    //一个pair类型,它的first元素具有const map<K,V>::key_type类型,second元素具有map<K,V>::mapped_type类型。

例:对map迭代器进行解引用时将产生pair类型的对象,对迭代器进行解引用,将获得一个引用,指向容器中一个value_type类型的值。对于map容器,其value_type是pair类型。

//get an iterator to an element in word_count
map<string, int>::iterator map_it = word_count.begin();
//map_it is areference to a pair<const string, int> object
cout << map_it->first;    //prints the key for this element
cout << " " <<map_it->second;    //prints the value of the element
map_it->first = "new key";    //error:key is const
++map_it->second;    //ok:we can change value through an iterator

3使用下表访问map对象

map<string, int>word_count;    //empty map
//insert default initialzed element with key Anna; then assign 1 to value
word_count["Anna"] = 1;
//上面两行代码将发生一下事件
//(1)在word_count中查找键为Anna的元素,没有找到
//(2)插入一个新的键值对,键为const string类型的对象,保存为Anna,值采用值初始化,即为0
//(3)将这个新的键值对插入到word_count中
//(4)读取新插入的元素,并将其值赋为1

使用下表访问map与使用下标访问数组或vector截然不同:当用下标访问不存在的元素时将导致在map容器中添加一个新的元素,该元素的键为该下标值。 

map的下标也可使用索引(键)来获取该键的值。

如果该键已在容器中,则map的下标运算与vector的下标运算相同:返回该键所关联的值。

如果在容器中找不到该键,map容器为该键创建一个新的元素,并将它插入到map对象中。此时,所关联的值采用值初始化:类类型的元素用默认构造函数初始化,而内置类型的元素则初始化为0

3.1下标操作符返回值的使用

cout << word_count["Anna"];    //fetch element indexed by Anna; prints 1
++ word_cout["Anna"];    //fetch the element and add one to it
cout << word_count["Anna"];    //fetch element and print it; prints 2

3.2下标行为的编程意义 

//对于map容器,如果下标所表示的键在容器中不存在,则添加新元素。这一特性可使程序惊人地简练
//count number of times each word occurs in the input
map<string, int> word_count;    //empty map from strign to int
string word;
while (cin >> word)
    ++word_count[word];
//这段代码创建了一个map对象,用来记录每个单词出现的次数
//while循环每次从标准输入读取一个单词。
//如果是一个新单词,在word_count中添加以该单词为索引(键)的新元素,所关联的值初始化为0,然后通过自增立即加1
//如果单词已存在,返回该键所关联的值,即单词的数量,然后通过自增立即加1.

G++编译例子

#include<iostream>
#include<string>
#include<map>
using std::map;
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main(){

	map<string, int> word_count;
	string word;
	while (cin >> word){
		++word_count[word];
		cout << word << "\t" << word_count[word] << endl;
	//for(string::size_type ix = 0; ix != word.size(); ++ix){
		//cout << word[ix] << "\t" << word_count["word[ix]"] <<endl;
	//}
	}
	return 0;
}

E:\>a
john jack tune chocolate tune anna anna anna
john    1
jack    1
tune    1
chocolate       1
tune    2
anna    1
anna    2
anna    3

 

//记录单词中字母出现的次数
#include<iostream>
#include<string>
#include<map>
using std::map;
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main(){

	map<char, int> word_count;
	string word;
	while (cin >> word){
	for(string::size_type ix = 0; ix != word.size(); ++ix){
		++ word_count[word[ix]];
	}
	for(string::size_type ix = 0; ix != word.size(); ++ix)
	cout << word[ix] << "\t" << word_count[word[ix]] <<endl;
	}
	return 0;
}

 E:\>a
streets
s       2
t       2
r       1
e       2
e       2
t       2
s       2
stls
s       4
t       3
l       1
s       4

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值