map介绍:
关联容器类,红黑树,默认按key升序排序,集合中每个元素是一个key、value对,key唯一,可通过key快速得到对应的value。
常用函数:
(1) 构造函数/赋值
map(); // 默认构造函数
map(const map& m) // 拷贝构造函数
map(iterator begin, iterator end ); //区间构造函数
map(iterator begin, iterator end, const traits& _compare) //带比较谓词的构造函数
map(iterator begin, iterator end, const traits& _compare, const allocator& all) //带分配器
(2) 增加函数/删除函数
clear 清空map
emplace 插入一个元素(不执行copy或move操作)到map
emplace_hint 插入一个元素(不执行copy或move操作)到map,有位置
erase 从map中移除指定位置或范围的数据
insert 在map的指定位置插入一或多个元素
eg:
m_map.insert(map<string,int>::value_type("hello",5));
m_map.insert(make_pair("hello",5));
mapTest.insert(pair<string, int>("string99999", 1));//insert-pair
(3) 遍历函数/访问函数
at 返回map中指定map键对应的map值
begin 返回指向map中第一个元素的迭代器
cbegin 返回指向map中第一个元素的const迭代器
cend 返回指向map的结尾位置之后位置的const迭代器
crbegin 返回指向map的反方向开始位置的const迭代器
crend 返回指向map的反方向结尾位置之后位置的const迭代器
end 返回指向map的结尾位置之后位置的迭代器
rbegin 返回指向map的反方向开始位置的迭代器
rend 返回指向map的反方向结尾位置之后位置的迭代器
equal_range 返回迭代器对,表示指定key的lower_bound和upp
lower_bound 返回指向map中大于等于指定key的第一个元素的位置的迭代器
upper_bound 返回指向map中大于指定key的第一个元素的位置的迭代器
operator[] 在map中插入一个元素,或访问一个已有元素
(4)查找/替换/比较
find 返回指向map中指定key的元素位置的迭代器
value_comp 返回用于对map的元素排序的比较对象的拷贝
key_comp 返回用于对map的key排序的比较对象的拷贝
(5) 判断函数
empty 如果map为空,返回true
(6) 大小函数/个数函数
count 返回map中指定键对应的元素个数
max_size 返回map的最大长度
size 返回map中的元素个数
(7) 其他函数
get_allocator 返回创建map的Allocator的拷贝
operator= 使用另一个map的拷贝替换map中的元素
swap 交换两个map中的元素
示例代码:
#include "stdafx.h"
#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
#include <functional>//greater<Type>必须包含此文件
#include <string>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
vector<pair<string, int>> vPair;
vPair.push_back(make_pair("bbbb", 2222));
vPair.push_back(make_pair("aaaa", 3333));
vPair.push_back(make_pair("aaaa", 565565));//重复key,不会push
vPair.push_back(make_pair("cccc", 4444));
vPair.push_back(make_pair("dddd", 5555));
map<string, int> mapTest;//默认以key为升序
mapTest.insert(vPair.begin(), vPair.end());//插入vector-pair
mapTest.insert(pair<string, int>("string99999", 1));//insert-pair
mapTest.insert(pair<string, int>("string8989", 165));
mapTest["jjjjk"] = 94943;//[]访问符号,即可插入新值,也可访问map;插入新值最好不使用此方法,其他方法效率更高
mapTest.insert(pair<string, int>("string1333", 145));
mapTest.insert(pair<string, int>("string15454", 156));
mapTest.emplace(pair<string, int>("test1test", 5565));//不执行copy构造
map<string, int>::iterator iter;
for (iter = mapTest.begin(); iter != mapTest.end(); iter++)
cout << iter->first << "," << iter->second << endl;
cout << " mapTest[\"dddd\"]:" << mapTest["dddd"] << endl;
cout << " mapTest.at(\"dddd\"):" << mapTest.at("dddd") << endl;
cout << "mapTest.find(\"dddd\"):" << mapTest.find("dddd")->first << "," << mapTest.find("dddd")->second << endl;
return 0;
}
输出:
aaaa,3333
bbbb,2222
cccc,4444
dddd,5555
jjjjk,94943
string1333,145
string15454,156
string8989,165
string99999,1
test1test,5565
mapTest["dddd"]:5555
mapTest.at("dddd"):5555
mapTest.find("dddd"):dddd,5555
请按任意键继续. . .
常用方法参考连接:http://www.howsoftworks.net/cplusplus.api/std/indexmap.html