C++中map提供的是一种键值对容器,里面的数据都是成对出现的,如下图:每一对中的第一个值称之为关键字(key),每个关键字只能在map中出现一次;第二个称之为该关键字的对应值。

——————————————————————————————————————————————
一. 声明
#include<map>
map<int, string> ID_Name;
map<int, string> ID_Name = {
{ 2015, "Jim" },
{ 2016, "Tom" },
{ 2017, "Bob" } };
——————————————————————————————————————————————
二. 插入操作
2.1 使用[ ]进行单个插入
map<int, string> ID_Name;
ID_Name[2015] = "Tom";
2.1 使用insert进行单个和多个插入
insert共有4个重载函数:
pair<iterator,bool> insert (const value_type& val);
iterator insert (const_iterator position, const value_type& val);
void insert (InputIterator first, InputIterator last);
void insert (initializer_list<value_type> il);
下面是具体使用示例:
#include <iostream>
#include <map>
int main()
{
std::map<char, int> mymap;
mymap.insert(std::pair<char, int>('a', 100));
mymap.insert(std::pair<char, int>('z', 200));
std::pair<std::map<char, int>::iterator, bool> ret;
ret = mymap.insert(std::pair<char, int>('z', 500));
if (ret.second == false) {
std::cout << "element 'z' already existed";
std::cout << " with a value of " << ret.first->second << '\n';
}
std::map<char, int>::iterator it = mymap.begin();
mymap.insert(it, std::pair<char, int>('b', 300));
mymap.insert(it, std::pair<char, int>('c', 400));
std::map<char, int> anothermap;
anothermap.insert(mymap.begin(), mymap.find('c'));
anothermap.insert({ { 'd', 100 }, {'e', 200} });
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
——————————————————————————————————————————————
三. 取值
Map中元素取值主要有at和[ ]两种操作,at会作下标检查,而[]不会。
map<int, string> ID_Name;
cout<<ID_Name[2016].c_str()<<endl;
ID_Name.at(2016) = "Bob";
——————————————————————————————————————————————
四. 容量查询
bool empty();
size_t size();
size_t max_size();
size_t count( const Key& key ) const;
——————————————————————————————————————————————
五. 迭代器
共有八个获取迭代器的函数:* begin, end, rbegin,rend* 以及对应的 * cbegin, cend, crbegin,crend*。
二者的区别在于,后者一定返回 const_iterator,而前者则根据map的类型返回iterator 或者 const_iterator。const情况下,不允许对值进行修改。如下面代码所示:
map<int,int>::iterator it;
map<int,int> mmap;
const map<int,int> const_mmap;
it = mmap.begin();
mmap.cbegin();
const_mmap.begin();
const_mmap.cbegin();
返回的迭代器可以进行加减操作,此外,如果map为空,则 begin = end。

——————————————————————————————————————————————
六. 删除交换
6.1 删除
iterator erase( iterator pos )
iterator erase( const_iterator first, const_iterator last );
size_t erase( const key_type& key );
void clear();
6.2 交换
void swap( map& other );
——————————————————————————————————————————————
七. 顺序比较
key_compare key_comp() const;
示例:
map<char,int> mymap;
map<char,int>::key_compare mycomp = mymap.key_comp();
mymap['a']=100;
mymap['b']=200;
mycomp('a', 'b');
——————————————————————————————————————————————
八. 查找
iterator find (const key_type& k);
const_iterator find (const key_type& k) const;
举例:
std::map<char,int> mymap;
std::map<char,int>::iterator it;
mymap['a']=50;
mymap['b']=100;
mymap['c']=150;
mymap['d']=200;
it = mymap.find('b');
if (it != mymap.end())
mymap.erase (it);
——————————————————————————————————————————————
九. 操作符
operator: == != < <= > >=
注意 对于==运算符, 只有键值对以及顺序完全相等才算成立。