[TOC]
文章参考:http://c.biancheng.net/view/7173.html
概述
C++ 中 map 提供的是一种键值对容器,里面的数据都是成对出现的,如下图:每一对中的第一个值称之为关键字(key),每个关键字只能在 map 中出现一次;第二个称之为该关键字的对应值。在一些程序中建立一个 map 可以起到事半功倍的效果,本文为大家总结了 map 的一些基本简单的操作!
构造函数
1 2 3 4 5
| #include <map>
std::map<int , std::string> mapPerson;
|
常用方法
插入数据
1 2 3 4 5 6 7 8
| std::map < int , std::string > mapPerson; mapPerson.insert(pair < int,string > (1,"Jim"));
mapPerson.insert(std::map < int, std::string > ::value_type (2, "Tom"));
mapPerson[3] = "Jerry";
|
数据遍历
三种最常用的遍历方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| std::map < int ,std::string > ::iterator it; std::map < int ,std::string > ::iterator itEnd; it = mapPerson.begin(); itEnd = mapPerson.end(); while (it != itEnd) { cout<<it->first<<' '<<it->second<<endl; it++; }
std::map < int, string > ::reverse_iterator iter; for(iter = mapPerson.rbegin(); iter != mapPerson.rend(); iter++) cout<<iter->first<<" "<<iter->second<<endl;
mapPerson.insert(std::map<int, std::string>::value_type (1, "Tom")); mapPerson[2] = "Jim"; mapPerson[3] = "Jerry";
int nSize = mapPerson.size(); for(int n = 1; n <= nSize; n++) qDebug()<<QString::fromStdString(mapPerson[n]);
|
注意:三种都是遍历,建议使用前向迭代器,慎用使用数组形成(角标开始位置谨慎)。
数据删除
1 2 3 4
| iterator erase(iterator it) ; iterator erase(iterator first,iterator last); size_type erase(const Key&key); clear();
|
swap方法
Map 中的 swap 不是一个容器中的元素交换,而是两个容器交换;
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 34 35
| #include <map> #include <iostream> using namespace std; int main( ) { map < int, int > m1, m2, m3; map < int, int >::iterator m1_Iter; m1.insert ( pair < int, int > ( 1, 10 ) ); m1.insert ( pair < int, int > ( 2, 20 ) ); m1.insert ( pair < int, int > ( 3, 30 ) ); m2.insert ( pair < int, int > ( 10, 100 ) ); m2.insert ( pair < int, int > ( 20, 200 ) ); m3.insert ( pair < int, int > ( 30, 300 ) ); cout << "The original map m1 is:"; for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ ) cout << " " << m1_Iter->second; cout << "." << endl; m1.swap( m2 ); cout << "After swapping with m2, map m1 is:"; for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ ) cout << " " << m1_Iter -> second; cout << "." << endl; cout << "After swapping with m2, map m2 is:"; for ( m1_Iter = m2.begin( ); m1_Iter != m2.end( ); m1_Iter++ ) cout << " " << m1_Iter -> second; cout << "." << endl; swap( m1, m3 ); cout << "After swapping with m3, map m1 is:"; for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ ) cout << " " << m1_Iter -> second; cout << "." << endl; }
|
sort函数
Map 中的元素是自动按 key 升序排序,所以不能对 map 用 sort 函数:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| #include <map> #include <iostream> using namespace std; int main( ) { map < int, int > m1; map < int, int >::iterator m1_Iter; m1.insert ( pair < int, int > ( 1, 20 ) ); m1.insert ( pair < int, int > ( 4, 40 ) ); m1.insert ( pair < int, int > ( 3, 60 ) ); m1.insert ( pair < int, int > ( 2, 50 ) ); m1.insert ( pair < int, int > ( 6, 40 ) ); m1.insert ( pair < int, int > ( 7, 30 ) ); cout << "The original map m1 is:"<<endl; for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ ) cout << m1_Iter->first<<" "<<m1_Iter->second<<endl; }
|
其他操作函数
表 1 列出了 map 容器提供的常用成员方法以及各自的功能。
| 成员方法 |
功能 |
| begin() |
返回指向容器中第一个(注意,是已排好序的第一个)键值对的双向迭代器。如果 map 容器用 const 限定,则该方法返回的是 const 类型的双向迭代器。 |
| end() |
返回指向容器最后一个元素(注意,是已排好序的最后一个)所在位置后一个位置的双向迭代器,通常和 begin() 结合使用。如果 map 容器用 const 限定,则该方法返回的是 const 类型的双向迭代器。 |
| rbegin() |
返回指向最后一个(注意,是已排好序的最后一个)元素的反向双向迭代器。如果 map 容器用 const 限定,则该方法返回的是 const 类型的反向双向迭代器。 |
| rend() |
返回指向第一个(注意,是已排好序的第一个)元素所在位置前一个位置的反向双向迭代器。如果 map 容器用 const 限定,则该方法返回的是 const 类型的反向双向迭代器。 |
| cbegin() |
和 begin() 功能相同,只不过在其基础上,增加了 const 属性,不能用于修改容器内存储的键值对。 |
| cend() |
和 end() 功能相同,只不过在其基础上,增加了 const 属性,不能用于修改容器内存储的键值对。 |
| crbegin() |
和 rbegin() 功能相同,只不过在其基础上,增加了 const 属性,不能用于修改容器内存储的键值对。 |
| crend() |
和 rend() 功能相同,只不过在其基础上,增加了 const 属性,不能用于修改容器内存储的键值对。 |
| find(key) |
在 map 容器中查找键为 key 的键值对,如果成功找到,则返回指向该键值对的双向迭代器;反之,则返回和 end() 方法一样的迭代器。另外,如果 map 容器用 const 限定,则该方法返回的是 const 类型的双向迭代器。 |
| lower_bound(key) |
返回一个指向当前 map 容器中第一个大于或等于 key 的键值对的双向迭代器。如果 map 容器用 const 限定,则该方法返回的是 const 类型的双向迭代器。 |
| upper_bound(key) |
返回一个指向当前 map 容器中第一个大于 key 的键值对的迭代器。如果 map 容器用 const 限定,则该方法返回的是 const 类型的双向迭代器。 |
| equal_range(key) |
该方法返回一个 pair 对象(包含 2 个双向迭代器),其中 pair.first 和 lower_bound() 方法的返回值等价,pair.second 和 upper_bound() 方法的返回值等价。也就是说,该方法将返回一个范围,该范围中包含的键为 key 的键值对(map 容器键值对唯一,因此该范围最多包含一个键值对)。 |
| empty() |
若容器为空,则返回 true;否则 false。 |
| size() |
返回当前 map 容器中存有键值对的个数。 |
| max_size() |
返回 map 容器所能容纳键值对的最大个数,不同的操作系统,其返回值亦不相同。 |
| operator[] |
map容器重载了 [] 运算符,只要知道 map 容器中某个键值对的键的值,就可以向获取数组中元素那样,通过键直接获取对应的值。 |
| at(key) |
找到 map 容器中 key 键对应的值,如果找不到,该函数会引发 out_of_range 异常。 |
| insert() |
向 map 容器中插入键值对。 |
| erase() |
删除 map 容器指定位置、指定键(key)值或者指定区域内的键值对。后续章节还会对该方法做重点讲解。 |
| swap() |
交换 2 个 map 容器中存储的键值对,这意味着,操作的 2 个键值对的类型必须相同。 |
| clear() |
清空 map 容器中所有的键值对,即使 map 容器的 size() 为 0。 |
| emplace() |
在当前 map 容器中的指定位置处构造新键值对。其效果和插入键值对一样,但效率更高。 |
| emplace_hint() |
在本质上和 emplace() 在 map 容器中构造新键值对的方式是一样的,不同之处在于,使用者必须为该方法提供一个指示键值对生成位置的迭代器,并作为该方法的第一个参数。 |
| count(key) |
在当前 map 容器中,查找键为 key 的键值对的个数并返回。注意,由于 map 容器中各键值对的键的值是唯一的,因此该函数的返回值最大为 1。 |