map

来源:互联网 发布:罗盘视频软件 编辑:程序博客网 时间:2024/06/11 14:14

39.map中key和value组合在一起构成键值对。

40.注意:key必须可以排序。(否则就必须进行重载)

41.键和值得对应关系必须一一对应,但是值和键则不一定。

42.map中插入已经有的键时,不一定能插入。

43.map不支持元素直接存取,因此元素的存取通常是经由迭代器进行。不过有个例外:map提供下标操作符,可直接存取元素。

43.当向map容器中插入一个键值对时,如果容器中已经存在此键值,insert的三种插入方式都失效,只有通过棕括号来修改键值对中值对应的值。

44.  Map中常用的一些函数:








39.代码如下:

   #include "Utility.h"

 

template <typename T>

void printContainer(T _container)

{

cout << "-------------" << endl;

for (T::const_iterator citer = _container.cbegin();

citer != _container.cend();

++citer)

{

//注意:map元素是由 key/value 构成,

//     (*citer).first 对应 key

//     (*citer).second 对应 value

cout << (*citer).first << "," << citer->second << endl;

}

}

 

/*

map容器:映射

1、只能存储一对一的映射,一对多的映射需要multimap容器实现

2、当插入一个已经存在于map内的key的新的数据时,插入失败,容器元素不会改变

*/

 

template<typename T>

void printContainer_Reverse(T _container)

{

cout << "*********************" << endl;

for (T::reverse_iterator rIter = _container.rbegin();

 rIter != _container.rend();

 ++rIter)

{

cout << (*rIter).first << "," << rIter->second << endl;

 

}

}

 

struct stItem

{

unsigned int ID;

unsigned int iBuyPrice;

 

//结构体相当于C语言的类,运算符重载 <

bool operator< (const stItem & item)const

{

return (ID <= item.ID);

}

};

 

void main()

{

//声明了一个map容器

map < char, int >  iMap1;

 

//向map中添加数据的方式1:使用[]来添加数据

iMap1['b'] = 1002;

iMap1['a'] = 1001;

iMap1['c'] = 1003;

printContainer(iMap1);

 

//向map中添加数据的方式2:map<char,int>::value_type('d', 1004)

iMap1.insert( map<char,int>::value_type('d', 1004) );

printContainer(iMap1);

 

//向map中添加数据的方式3:pair<char,int>('e', 1004)

iMap1.insert( pair<char,int>('e', 1005) );

printContainer(iMap1);

 

//向map中添加数据的方式4:make_pair('f', 1006)

iMap1.insert(make_pair('f', 1006));

printContainer(iMap1);

 

 

//------map容器的 value = container.at( key )函数--------

cout << "------map容器的 value = container.at(key )函数--------" << endl;

char search = 'c';

cout << search << " 对应:" << iMap1.at(search) << endl;

 

//------map容器特有的函数: count()--------

cout << "--------count()--------" << endl;

if ( iMap1.count('c') )

cout << "找到c元素" << endl;

else

cout << "count() error" << endl;

 

//------map容器特有的函数: find()--------

//如果找到返回该元素的迭代器,否则返回end()

cout << "--------find()--------" << endl;

map<char, int>::iterator findIter;

findIter = iMap1.find('d');

if ( findIter == iMap1.end() )

cout << "find() error" << endl;

else

cout << "找到!" << (*findIter).first <<":" << findIter->second << endl;

 

 

map<char, int> iMap2;

iMap2['a'] = 1001;

//    'b'

//  'c'

iMap2.insert(map<char, int>::value_type('d', 1004));

//  'e'

//  'f'

//    'g'

iMap2.insert(pair<char,int>('h', 1008));

//  'i'

//  'j'

//    'k'

//  'l'

iMap2.insert(make_pair('m', 1013));

printContainer(iMap2);

 

//------map容器特有的函数: lower_bound()--------

//返回第一个>=key的元素的迭代器

cout << "--------lower_bound()--------" << endl;

map<char, int>::iterator lowerIter, upperIter;

lowerIter = iMap2.lower_bound('d');

cout << "找到!  " << (*lowerIter).first <<":" << lowerIter->second << endl;

 

//------map容器特有的函数: upper_bound()--------

//返回第一个>key的元素的迭代器

cout << "--------upper_bound()--------" << endl;

upperIter = iMap2.upper_bound('d');

cout << "找到!  " << (*upperIter).first <<":" << upperIter->second << endl;

 

//------map容器特有的函数: equal_range()--------

//返回一个pair,分别是lower_bound的返回值与upper_bound的返回值

//如果

cout << "--------equal_range()--------" << endl;

pair< map<char, int>::iterator, map<char, int>::iterator > range;

char equal_rangeSearch = 'p';

range = iMap2.equal_range(equal_rangeSearch);

if ( range.first == range.second )

cout << "容器中没有key为" << equal_rangeSearch <<"的元素"<< endl;

else

{

cout << range.first->first  << ":" << range.first->second << endl;

cout << range.second->first << ":" << range.second->second << endl;

}

 

 

//--------------------------------

map<int, char> iMap3;

iMap3[1] = 'A';

iMap3[2] = 'B';

iMap3[4] = 'C';

iMap3[6] = 'D';

printContainer(iMap3);

 

if (!iMap3.count(4))

iMap3[4] = 'M'; //会覆盖原有数据

printContainer(iMap3);

 

//-----------Map容器的排序准则为:升序------------

/*

注意:key值要保证能够进行比较

*/

map<stItem, string> items;

stItem item1 = { 1001, 8000 };

stItem item2 = { 1002, 16000 };

stItem item3 = { 1003, 24000 };

items[item1] = "倚天剑";

items[item2] = "倚天天剑";

items[item3] = "倚天天天剑";

 

for (map<stItem, string>::iterator iter = items.begin();

iter != items.end();

++iter)

{

cout << (*iter).second << " : ID = " << iter->first.ID <<" , BuyPrice:" << iter->first.iBuyPrice << endl;

}

 

 

 

system("pause");

}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 太阳凹颧骨外扩怎么办 4岁儿童脊柱侧弯怎么办 瘦的人得多囊怎么办 智齿刚长出来该怎么办 宝宝耳朵睡尖了怎么办 睡觉压的耳朵疼怎么办 月子里奶水越来越少怎么办 月子里生气回奶了怎么办 儿童疫苗本丢了怎么办 跖骨2-5骨折了怎么办 耳朵被水堵住了怎么办 耳朵一直流黄水怎么办 两个月宝宝脐疝怎么办 拔牙后一直渗血怎么办 耳朵滴药水堵了怎么办 刚打的耳洞化脓怎么办 耳朵进水了一直嗡嗡响怎么办 婴儿游泳呛水了怎么办 孩子游泳呛水了怎么办 婴儿洗澡呛水了怎么办 小孩脸上长湿疹老是不好怎么办 油耳堵住了耳朵怎么办 耳屎突然变湿该怎么办 小孩有耳屎好硬怎么办 小孩的耳屎深硬怎么办 1岁宝宝喉咙发炎怎么办 牙旁边的肉疼怎么办 鼻头软骨捏的痛怎么办 耳洞发炎肿了怎么办 一上火耳朵就疼怎么办 耳朵像隔了层膜怎么办 感冒引起的耳闷怎么办 5岁儿童视力0.6怎么办 柯基耳朵不立怎么办 宝宝一惊一乍睡觉不踏实怎么办 新婴儿睡觉不踏实怎么办 耳朵里面疼肿了怎么办 生出来的孩子是畸形怎么办 二胎生了缺陷儿怎么办 扣完肚脐眼后疼怎么办 刚出生的婴儿屁股红怎么办