map
map是STL的一个关联容器,它提供一对一(key-value)的数据处理能力。其中key为关键字,每个关键字只能在map中出现一次,value为该关键字的值。
键可以是基本类型,也可以是类类型。字符串经常被用来作为键,如果想要保存姓名和地址的记录,就可以这么使用。
map内部自建一颗红黑树,这颗树具有对数据自动排序的功能,所以在map内部所有的数据都是有序的。
新增和更新
有四种方式给map增加元素:
// 假设map由string和int组成,以下例程来源于参考资料
// 1) Assignment using array index notation
Foo["Bar"] = 12345;
// 2) Assignment using member function insert() and STL pair
Foo.insert(std::pair<string,int>("Bar", 12345));
// 3) Assignment using member function insert() and "value_type()"
Foo.insert(map<string,int>::value_type("Bar", 12345));
// 4) Assignment using member function insert() and "make_pair()"
Foo.insert(std::make_pair("Bar", 12345));
其中,有一个是以数组形式[]
,其余三个都是使用成员函数insert
。这两种形式在语义就是不同的。
[]
会覆盖原有的值(如果key已经存在),总是需要用新值更新数据时使用insert
会忽略新值(如果key已经存在),不需要重复的数据时使用
对于后三种insert
形式:
std::map<std::string, int>::value_type
就是std::pair<std::string const, int>
,所以3)和4)基本一样std::make_pair("Bar", 12345)
比std::pair<std::string, int>("Bar", 12345)
高效,因为string是一个完整的类,需要进行copy操作,而“bar”只是一个字符串(指针的副本),但最终仍要创建string,所以它还是取决于编译器- 另外,
std::make_pair
更简短且没有重复一遍map的定义
综上,如果需要更新数据,使用[]
,如果不需要重复的数据,使用insert(std::make_pair)
。
当然,c11提供了另一个选择,map.emplace("Bar", 12345);
,它的行为与insert
一样,但快且简单。
查找
查找一般分为两种情况:
- 只需确认是否存在指定的key,返回bool
- 需要对指定的key的元素操作,返回指向该元素的迭代器
使用下面的代码片断即可:
// 返回bool
// 使用find
return cars.find(name) != cars.end();
// 使用count
return cars.count(name) != 0;
// 返回迭代器
auto element = cars.find(name);
if (element != cars.end())
{
std::cout << cars->first << std::endl;
}
else
{
std::cout << "not found: " << name << std::endl;
}
删除
删除map中的元素通常使用erase()
和clear()
。
erase()
用于删除指定的键值对,可以指定位置、值或者成员区间,用法如下:
- erase(key),删除key指定的元素,并重新排序。返回删除的元素个数,如果不存在该key指定的元素,则返回0
- erase(iter),删除迭代器位置的元素
- erase(strt_iter, end_iter) ,删除起始和终止范围内所有元素
示例代码如下:
// C++ code to demonstrate the working of erase()
#include<iostream>
#include<map> // for map operations
using namespace std;
int main()
{
// declaring map
// of char and int
map< char, int > mp;
// declaring iterators
map<char, int>::iterator it ;
map<char, int>::iterator it1;
map<char, int>::iterator it2;
// inserting values
mp['a']=5;
mp['b']=10;
mp['c']=15;
mp['d']=20;
mp['e']=30;
// printing initial map elements
cout << "The initial map elements are : \n";
for (it1 = mp.begin(); it1!=mp.end(); ++it1)
cout << it1->first << "->" << it1->second << endl;
it = mp.begin();
cout << endl;
// erasing element using iterator
// erases 2nd element
// 'b'
++it;
mp.erase(it);
// printing map elements after deletion
cout << "The map elements after 1st deletion are : \n";
for (it1 = mp.begin(); it1!=mp.end(); ++it1)
cout << it1->first << "->" << it1->second << endl;
cout << endl;
// erasing element using value
int c = mp.erase('c');
// printing map elements after deletion
cout << "The map elements after 2nd deletion are : \n";
for (it1 = mp.begin(); it1!=mp.end(); ++it1)
cout << it1->first << "->" << it1->second << endl;
cout << "The number of elements deleted in 2nd deletion are : ";
cout << c << endl;
cout << endl;
// erasing element using value
// key not present
int d = mp.erase('w');
// printing map elements after deletion
cout << "The map elements after 3rd deletion are : \n";
for (it1 = mp.begin(); it1!=mp.end(); ++it1)
cout << it1->first << "->" << it1->second << endl;
cout << "The number of elements deleted in 3rd deletion are : ";
cout << d << endl;
cout << endl;
++it;
++it;
// erasing element using range iterator
// deletes "d" and "e" keys
mp.erase(it, mp.end());
// printing map elements 4th deletion
cout << "The map elements after 4th deletion are : \n";
for (it1 = mp.begin(); it1!=mp.end(); ++it1)
cout << it1->first << "->" << it1->second << endl;
cout << endl;
}
输出如下:
The initial map elements are :
a->5
b->10
c->15
d->20
e->30
The map elements after 1st deletion are :
a->5
c->15
d->20
e->30
The map elements after 2nd deletion are :
a->5
d->20
e->30
The number of elements deleted in 2nd deletion are : 1
The map elements after 3rd deletion are :
a->5
d->20
e->30
The number of elements deleted in 3rd deletion are : 0
The map elements after 4th deletion are :
a->5
clear()
用于清空map,该函数执行后,会删除map中所有元素,map的size为0。
参考资料
Most efficient way to assign values to maps
Different ways to delete elements in std::map (erase() and clear())