简介
Map是一种关联容器,它存储的元素类型是pair型的键值对,它提供了很好的一对一的对应关系。
成员函数
复制控制
map::map()
构造函数:构造一个map容器,根据构造函数版本初始化容器的内容。
map::~map()
析构函数:释放一个map容器对象。
map::operator=
赋值函数:为容器分配新的内容以代替其当前内容。
示例代码
#include <iostream>
#include <map>
using namespace std;
void print(map<char, int> m)
{
for(map<char, int>::iterator it = m.begin();
it != m.end(); ++it){
cout << it -> first << " -> " << it -> second;
cout << endl;
}
}
bool comp(char first, char second)
{
return first > second;
}
class reverse{
public:
bool operator()(const char& first, const char& second) const
{
return first > second;
}
};
int main(void)
{
// map::map()
map<char, int> first;
first['a'] = 10;
first['c'] = 30;
first['b'] = 20;
print(first);
map<char, int> second(first.begin(), first.end());
print(second);
map<char, int> third(second);
print(second);
map<char, int, reverse> fourth;
fourth['a'] = 100;
fourth['b'] = 200;
fourth['c'] = 300;
for(map<char, int, reverse>::iterator it = fourth.begin();
it != fourth.end(); ++it){
cout << it -> first << " -> " << it -> second << endl;
}
map<char, int, bool (*)(char, char)> fifth(comp);
fifth['a'] = 1;
fifth['b'] = 2;
fifth['c'] = 3;
for(map<char, int, bool (*)(char, char)>::iterator it
= fifth.begin(); it != fifth.end(); ++it){
cout << it -> first << " -> " << it -> second << endl;
}
// map::operator=
map<char, int> sixth;
sixth = first;
print(sixth);
return 0;
}
Iterators
map::begin()
返回一个迭代器,指向map容器的第一个元素。返回值类型iterator/const_iterator。
map::end()
返回一个迭代器,指向map容器最后一个元素的下一个位置。返回值类型iterator/const_iterator。
map::rbegin()
返回一个反转迭代器,指向map容器最后一个元素。返回值类型为reverse_iterator/const_reverse_iterator。
map::rend()
返回一个反转迭代器,指向map容器第一个元素的前一个位置。返回值类型为reverse_iter/const_reverse_iterator。
map::cbegin()
begin()的const版本。
map::cend()
end()的const版本。
map::crbegin()
rbegin()的const版本。
map::crend()
rend()的const版本。
示例程序
#include <iostream>
#include <map>
using namespace std;
int main(void)
{
map<char, int> first;
first['a'] = 1;
first['b'] = 2;
first['c'] = 3;
// map::begin() end()
for(map<char, int>::iterator it = first.begin();
it != first.end(); ++it){
cout << it -> first << " -> " << it -> second;
cout << endl;
}
// map::rbegin() rend()
for(map<char, int>::reverse_iterator it = first.rbegin();
it != first.rend(); ++it){
cout << it -> first << " -> " << it -> second;
cout << endl;
}
// map::cbegin() cend()
for(map<char, int>::const_iterator it = first.cbegin();
it != first.cend(); ++it){
cout << it -> first << " -> " << it -> second;
cout << endl;
}
// map::crbegin() crend()
for(map<char, int>::const_reverse_iterator
it = first.crbegin(); it != first.crend(); ++it){
cout << it -> first << " -> " << it -> second;
cout << endl;
}
return 0;
}
Capacity
map::empty()
判断容器是否为空。容器为空返回true,否则返回false。
map::size()
返回容器中元素个数。
map::max_size()
返回map容器能存储元素的最大个数。
示例程序
#include <iostream>
#include <map>
using namespace std;
int main(void)
{
map<char, int> first;
first['a'] = 1;
first['b'] = 2;
first['c'] = 3;
// map::empty()
if(first.empty()){
cout << "The first is empty" << endl;
}else{
cout << "The first is not empty" << endl;
}
// map::size()
cout << "The size of first is " << first.size() << endl;
// map::max_size()
cout << "The maximum of element : " << first.max_size();
cout << endl;
return 0;
}
Element_access
map::operator[]
返回一个mapped_type对象的引用。
map::at()
返回一个mapped_type对象的应用。
示例程序
#include <iostream>
#include <map>
using namespace std;
int main(void)
{
map<char, int> first;
first['a'] = 1;
first['b'] = 2;
first['c'] = 3;
// map::operator[]
cout << first['a'] << endl;
// map::at
first.at('a') = 20;
first.at('b') = 30;
for(auto &x : first ){
cout << x.first << " -> " << x.second;
cout << endl;
}
return 0;
}
Modifiers
map::insert()
在map容器指定位置插入一个或一组元素。
map::erase()
删除map容器中一个或一组元素。
map::swap()
交换两个容器的内容,两个容器的类型必须相同大小可以不同。
map::clear()
删除容器中的所有元素。
map::emplace()
向容器中插入一个新的元素,插入的元素由其参数的构造函数创建。返回值为一个pair对象。
map::emplace_hint()
向容器指定位置插入一个新的元素,插入的元素由其参数的构造函数创建。返回值为一个iterator。
示例程序
#include <iostream>
#include <map>
using namespace std;
void print(map<char, int> m)
{
for(auto &x : m){
cout << x.first << " " << x.second << endl;
}
}
int main(void)
{
map<char, int> first;
// map::insert()
first.insert(pair<char, int>('a', 1));
first.insert(pair<char, int>('z', 2));
pair<map<char, int>::iterator, bool> ret;
ret = first.insert(pair<char, int>('z', 3));
if(ret.second == false){
cout << "Element z is already existed";
cout << " with a value of " << ret.first->second;
cout << endl;
}
map<char, int>::iterator it = first.begin();
first.insert(it, pair<char, int>('b', 3));
first.insert(it, pair<char, int>('c', 4));
print(first);
map<char, int> second;
second.insert(first.begin(), first.find('c'));
print(second);
// map::erase()
it = first.find('z');
first.erase(it);
first.erase('a');
first.erase(first.begin(), first.find('c'));
print(first);
// map::swap()
first.insert(pair<char, int>('a', 1));
cout << "before swap : " << endl;;
cout << "first : " << endl;
print(first);
cout << "second : " << endl;
print(second);
first.swap(second);
cout << "first : " << endl;
print(first);
cout << "second : " << endl;
print(second);
// map::clear()
first.clear();
// map::emplace()
first.emplace('a', 10);
print(first);
// map::emplace_hint()
it = first.begin();
it = first.emplace_hint(it, 'd', 20);
first.emplace_hint(it, 'e', 30);
first.emplace_hint(first.begin(), 'f', 40);
print(first);
return 0;
}
#include <iostream>
#include <map>
using namespace std;
void print(map<char, int> m)
{
for(auto &x : m){
cout << x.first << " " << x.second << endl;
}
}
int main(void)
{
map<char, int> first;
// map::insert()
first.insert(pair<char, int>('a', 1));
first.insert(pair<char, int>('z', 2));
pair<map<char, int>::iterator, bool> ret;
ret = first.insert(pair<char, int>('z', 3));
if(ret.second == false){
cout << "Element z is already existed";
cout << " with a value of " << ret.first->second;
cout << endl;
}
map<char, int>::iterator it = first.begin();
first.insert(it, pair<char, int>('b', 3));
first.insert(it, pair<char, int>('c', 4));
print(first);
map<char, int> second;
second.insert(first.begin(), first.find('c'));
print(second);
// map::erase()
it = first.find('z');
first.erase(it);
first.erase('a');
first.erase(first.begin(), first.find('c'));
print(first);
// map::swap()
first.insert(pair<char, int>('a', 1));
cout << "before swap : " << endl;;
cout << "first : " << endl;
print(first);
cout << "second : " << endl;
print(second);
first.swap(second);
cout << "first : " << endl;
print(first);
cout << "second : " << endl;
print(second);
// map::clear()
first.clear();
// map::emplace()
first.emplace('a', 10);
print(first);
// map::emplace_hint()
it = first.begin();
it = first.emplace_hint(it, 'd', 20);
first.emplace_hint(it, 'e', 30);
first.emplace_hint(first.begin(), 'f', 40);
print(first);
return 0;
}
Observers
map::key_comp()
返回一个key_compare对象,用于map键的比较。
map::value_comp()
返回一个value_compare对象,用于map值的比较。
示例程序
#include <iostream>
#include <map>
using namespace std;
int main(void)
{
map<char, int> first;
first.insert(pair<char, int>('a', 1));
first.insert(pair<char, int>('b', 2));
first.insert(pair<char, int>('c', 3));
// map::key_comp()
map<char, int>::key_compare comp = first.key_comp();
auto key_end = first.rbegin() -> first;
auto it = first.begin();
do{
cout << it -> first << " -> " << it -> second;
cout << endl;
}while(comp((*it++).first, key_end));
// map::value_comp()
pair<char, int> value_end = *first.rbegin();
it = first.begin();
do{
cout << it -> first << " -> " << it -> second;
cout << endl;
}while(first.value_comp()(*it++, value_end));
return 0;
}
Operators
map::find()
根据key在容器中查找一个元素,找到返回一个指向该元素的迭代器,否则返回一个迭代器指向容器最后一个元素的下一个位置。
map::count()
返回与key值匹配元素的个数,返回值类型为size_type。
map::lower_bound()
返回指向大于或等于某个值的第一个元素的迭代器
map::upper_bound()
返回指向大于某个值的迭代器。
map::equal_range()
返回集合中与给定值相等的上下限的两个迭代器。
示例程序
#include <iostream>
#include <map>
using namespace std;
int main(void)
{
map<char,int> m;
m.insert(pair<char,int>('a',1));
m.insert(pair<char,int>('b',2));
m.insert(pair<char,int>('c',3));
m.insert(pair<char,int>('d',4));
m.insert(pair<char,int>('e',5));
// map::find()
auto it = m.find('d');
if(it != m.end()){
cout << "Find it." << endl;
}else{
cout << "Not found." << endl;
}
// map::count()
cout << "The number of element is " << m.count('a');
cout << endl;
// map::lower_bound() upper_bound()
auto itlow = m.lower_bound('b');
auto itupper = m.upper_bound('d');
m.erase(itlow, itupper);
for(auto &x : m){
cout << x.first << " " << x.second << endl;
}
// map::equal_range()
pair<map<char,int>::iterator, map<char,int>::iterator> ret;
ret = m.equal_range('a');
cout << "Lower bound point to ";
cout << ret.first -> first << "->" << ret.first -> second;
cout << endl;;
cout << "Upper bound point to ";
cout << ret.second -> first << "->" << ret.second -> second;
cout << endl;
return 0;
}