[c++ primer] 关联容器_map

map 是键-值对的集合。map类型通常可以理解为关联数组,其本质在于元素的值和某个特定的键向关联,而非通过元素在数组中的位置来获取


声明与定义:

map<int ,int >  m;

map<string,int> word_count;

迭代器:

map<string,int>:: iterator it;


对键类型的约束:键类型必须支持"<"操作符


map定义的类型

1.迭代器进行解引用将产生pair 类型的对象

其 first 成员存放键,为const 而second 成员则存放值

2. map额外定义的类型别名

map<string,int> ::key_type    为其键类型,相当于   string   

map<string,int> ::mapped_type    为其值类型,相当于   int



添加元素:

1.使用下标访问map对象

word_count["kaixin"]  = 1;

这一句话会先在map中查找是否存在kaixin 这个键,如果存在,则把其值赋值为1

如果不存在,其会kaixin插入到map中,然后把其值初始化,然后再给其赋值为1

(1)下标操作符返回值的使用

map迭代器返回是一个value_type类型的值,其为pair类型,包含const key_type

和 mapped_type 

下标操作符则返回一个mapped_type类型

(2).下标行为的编程意义

#include <iostream>
#include <string>
#include <map>
using namespace std;
map<string,int>  word_count;
map<string,int>::iterator it;
int main()
{
	string word;
	while(cin >> word)
		++word_count[word];//输入并统计
	
	//输出	 
	for(it=word_count.begin();it!=word_count.end();it++)
		cout << it->first << " "<<it->second<<endl;
	return 0;
}


2.map::insert的使用:

1.

typedef map<string,int>::value_type  valType;

word_count.insert(valType("kaixin",1));

2.

word_count.insert(make_pair("kaixin",1));



insert(e);函数存在返回值,返回一个pair类型,包括(pair<string,int>::iterator 和 bool)

#include <iostream>
#include <string>
#include <map>
using namespace std;
map<string,int>  word_count;
map<string,int>::iterator it;
int main()
{
	string word;
	while(cin >> word)
	{
		pair<map<string,int>::iterator,bool> ret = 
			word_count.insert(make_pair(word,1));//如果word存在,返回指向word的迭代器,不做任何操作 
												//否则插入并返回指向刚刚插入元素的迭代器 
		if(!ret.second)//如果没有插入进去,代表map中已有该元素 则相应的标准位++ 
			++ret.first->second; 
	} //输入并统计
	
	//输出	 
	for(it=word_count.begin();it!=word_count.end();it++)
		cout << it->first << " "<<it->second<<endl;
	return 0;
}





查找并读取map中元素

1.直接用下标读取    但副作用是如果该元素不存在,就会插入一个新的元素

2.m.count(k)

返回m中k出现的次数,其返回值只能为0或1   ,可以判断该元素是否存在

3.m.find(k)

返回指向元素的迭代器,如果元素不存在,返回指向。m.end()的迭代器


从map对象中删除元素

m.erase(k)     删除m中键为k的元素,返回size_type类型的值,表示删除元素的个数

m.erase(p)     删除迭代器p所指向的元素,p中必须存在m中

m.erase(b,e)  删除一段范围内的元素


#include <iostream>
#include <string>
#include <map>
using namespace std;
map<string,int>  word_count;
map<string,int>::iterator it;
int main()
{
	string word;
	while(cin >> word)
	{
		pair<map<string,int>::iterator,bool> ret = 
			word_count.insert(make_pair(word,1));//如果word存在,返回指向word的迭代器,不做任何操作 
												//否则插入并返回指向刚刚插入元素的迭代器 
		if(!ret.second)//如果没有插入进去,代表map中已有该元素 则相应的标准位++ 
			++ret.first->second; 
	} //输入并统计
	
	word_count.erase(word_count.begin());  //删除第一个元素 
	
	printf("\n\nthe print %d:\n",word_count.size());
	 
	//输出	 
	for(it=word_count.begin();it!=word_count.end();it++)
		cout << it->first << " "<<it->second<<endl;
	
	word_count.erase(word_count.begin(),word_count.end());
	//清空map 
	printf("\n\nthe print %d:\n",word_count.size());
	//输出	 
	for(it=word_count.begin();it!=word_count.end();it++)
		cout << it->first << " "<<it->second<<endl;
	
	return 0;
}


map对象的迭代遍历:

map<string,int>::iterator it;
for(it=word_count.begin();it!=word_count.end();it++)
		cout << it->first << " "<<it->second<<endl;
	














  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值