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");
}