总结
map用于数据的集合,在这个集合中的每个元素均为包含值(value)和键(key)的元素对(pair)。 键(key)的值是唯一的,并且在map中每次插入或删除都会自动排序。map中的元素值(value)可以直接更改。 键(key)是常量,不能更改。 必须先删除与旧元素关联的键值,才能为新元素插入新键值。
在使用map是需要包含头文件<map>以及命名空间std:
#include <map>
using namespace std;
c++标准库map类的特点包括有:
- 一个可变大小的容器,可以根据相关联的键值高效地检索元素值。
- 可逆,因为它提供了双向迭代器来访问它的元素。
- 有序,map内部自建一颗红黑树(一 种非严格意义上的平衡二叉树),这颗树根据键值(key)对数据进行自动排序,所以在map内部所有的数据都是有序的。
- 唯一。因为它的每个元素必须有一个唯一的键。
- 关联容器对,因为它的元素数据值与键值不同。
- 模板类,因为它提供的功能是通用的,并且独立于元素或键类型。用于元素和键的数据类型与比较函数和分配器一起在类模板中指定为参数。
template <class Key, //要存储在map中的键数据类型(key)。
class Type, //要存储在映射中的元素数据类型(value)。
class Traits = less<Key>, //提供函数对象的类型,该函数对象可以将两个元素值作为排序键进
//行比较,以确定它们在映射中的相对顺序。这个参数是可选的,默
//认值是二元谓词 less<Key>
class Allocator=allocator<pair <const Key, Type>>>
//表示存储的分配器对象的类型,该对象封装有关映射内存的分配和
//释放的详细信息。这个参数是可选的,默认值
//allocator<pair<const Key, Type> >。
class map;
成员
typedef
类型名称 | 描述 |
---|---|
allocator_type | map对象的 allocator 类的类型定义。 |
const_iterator | 一个双向迭代器可以读取map中的const元素。 |
const_pointer | 一个指针可以指向map中的const元素。 |
const_reference | 用于引用存储在map中的const元素,用于读取和执行const操作。 |
const_reverse_iterator | 一种提供双向迭代器的类型,可以读取map中的任何const元素。 |
difference_type | 有符号整数类型,用于迭代器指向的元素之间范围内的映射元素数。 |
Iterator | 双向迭代器,可以读取或修改映射中的任何元素。 |
key_compare | 函数对象,可以比较两个排序键以确定映射中两个元素的相对顺序。 |
key_type | 存储在map的每个元素中的排序键的类型。 |
mapped_type | 存储在map的每个元素中的数据的类型。 |
pointer | 用于指向map中const元素的指针的类型。 |
reference | 用于引用存储在map中的元素的类型。 |
reverse_iterator | 用于双向迭代器的类型,可以读取或修改反转map中的元素。 |
size_type | 无符号整数typedef,用于表示map中的元素数量 |
value_type | 作为map中元素存储的对象类型的类型。 |
成员函数
成员函数 | 描述 |
---|---|
at | 查找具有指定键值的元素。 |
begin | 返回一个迭代器,此迭代器指向map中的第一个元素。 |
cbegin | 返回一个常量迭代器,此迭代器指向map中的第一个元素。 |
cend | 返回一个超过末尾的常量迭代器。 |
clear | 清除map的所有元素。 |
count | 返回map中其键与参数中指定的键匹配的元素数量。 |
crbegin | 返回一个常量迭代器,此迭代器指向反向map中的第一个元素。 |
crend | 返回一个常量迭代器,此迭代器指向反向map中最后一个元素之后的位置。 |
emplace | 将就地构造的元素插入到map。 |
emplace_hint | 将就地构造的元素插入到map,附带位置提示。 |
empty | 返回 true则map为空。 |
end | 返回超过末尾的迭代器。 |
equal_range | 返回一对迭代器。 此迭代器对中的第一个迭代器指向 map 中其键大于指定键的第一个元素。 此迭代器对中的第二个迭代器指向 map 中其键等于或大于指定键的第一个元素。 |
erase | 从指定位置移除map中的元素或元素范围。 |
find | 返回一个迭代器,此迭代器指向map中其键与指定键相等的元素的位置。 |
get_allocator | 返回用于构造map的 allocator 对象的副本。 |
insert | 将元素或元素范围插入到map中的指定位置。 |
key_comp | 返回用于对map中的键进行排序的比较对象副本。 |
lower_bound | 返回一个迭代器,此迭代器指向map中其键值等于或大于指定键的键值的第一个元素。 |
max_size | 返回map的最大长度。 |
rbegin | 返回一个迭代器,此迭代器指向反向map中的第一个元素。 |
rend | 返回一个迭代器,此迭代器指向反向map中最后一个元素之后的位置。 |
size | 返回map中的元素数量。 |
swap | 交换两个map的元素。 |
upper_bound | 返回一个迭代器,此迭代器指向映射中其键值大于指定键的键值的第一个元素。 |
value_comp | 检索用于对map中的元素值进行排序的比较对象副本。 |
运算符
运算符 | 描述 |
---|---|
operator[] | 将元素插入到具有指定键值的map。 |
operator= | 将一个map中的元素替换为另一map副本。 |
构造函数
map();
explicit map(
const Traits& Comp); //Comp:用于对 map 中元素排序的类型 const Traits 的比较函数,默认
//为 hash_compare。
map(
const Traits& Comp,
const Allocator& Al); //Al:要用于此 map 对象的存储分配器类,默认为 Allocator。
map(
const map& Right); //Right:所构造集要作为其副本的 map 。
map(
map&& Right);
map(
initializer_list<value_type> IList); //IList:要从中复制元素的 initializer_list。
map(
initializer_list<value_type> IList,
const Traits& Comp);
map(
initializer_list<value_type> IList,
const Traits& Comp,
const Allocator& Allocator);
template <class InputIterator>
map(
InputIterator First, //First:要复制的范围元素中的第一个元素的位置。
InputIterator Last); //Last:要复制的元素范围以外的第一个元素的位置。
template <class InputIterator>
map(
InputIterator First,
InputIterator Last,
const Traits& Comp);
template <class InputIterator>
map(
InputIterator First,
InputIterator Last,
const Traits& Comp,
const Allocator& Al);
示例
#include <map>
#include <iostream>
int main()
{
using namespace std;
typedef pair <int, int> Int_Pair;
map <int, int>::iterator m1_Iter, m3_Iter, m4_Iter, m5_Iter, m6_Iter, m7_Iter;
map <int, int, less<int> >::iterator m2_Iter;
// 创建键值类型均为int的空map m0
map <int, int> m0;
// 使用less<int>的比较函数创造一个空map:m1,然后插入四个元素。
map <int, int, less<int> > m1;
m1.insert(Int_Pair(1, 10));
m1.insert(Int_Pair(2, 20));
m1.insert(Int_Pair(3, 30));
m1.insert(Int_Pair(4, 40));
// 使用less<int>的比较函数创造一个空map:m2,然后插入四个元素。
map <int, int, less<int> > m2;
m2.insert(Int_Pair(1, 10));
m2.insert(Int_Pair(2, 20));
// 使用m1的分配器创造map:m3
map <int, int>::allocator_type m1_Alloc;
m1_Alloc = m1.get_allocator();
map <int, int> m3(less<int>(), m1_Alloc);
m3.insert(Int_Pair(3, 30));
// 创建一个map:m4,拷贝自map:m1
map <int, int> m4(m1);
// 从m1[ first, last)这个范围内创建m5
map <int, int>::const_iterator m1_bcIter, m1_ecIter;
m1_bcIter = m1.begin();
m1_ecIter = m1.begin();
m1_ecIter++;
m1_ecIter++;
map <int, int> m5(m1_bcIter, m1_ecIter);
// 通过复制m4 [first,last)的范围和m2的分配器来创建m6
map <int, int>::allocator_type m2_Alloc;
m2_Alloc = m2.get_allocator();
map <int, int> m6(m4.begin(), ++m4.begin(), less<int>(), m2_Alloc);
// 通过移动m5创建一个m7
cout << "m7 =";
map<int, int> m7(move(m5));
for (auto i : m7)
cout << i.first << " " << i.second << ", ";
cout << endl;
// 通过复制initializer_list创建m8
map<int, int> m8{ { { 1, 1 }, { 2, 2 }, { 3, 3 }, { 4, 4 } } };
cout << "m8: = ";
for (auto i : m8)
cout << i.first << " " << i.second << ", ";
cout << endl;
// 使用initializer_list和比较器创建m9
map<int, int> m9({ { 5, 5 }, { 6, 6 }, { 7, 7 }, { 8, 8 } }, less<int>());
cout << "m9: = ";
for (auto i : m9)
cout << i.first << " " << i.second << ", ";
cout << endl;
// 使用initializer_list,比较器和分配器创建m10
map<int, int> m10({ { 9, 9 }, { 10, 10 }, { 11, 11 }, { 12, 12 } }, less<int>(), m9.get_allocator());
cout << "m10: = ";
for (auto i : m10)
cout << i.first << " " << i.second << ", ";
cout << endl;
}
成员函数的用法
map::at
函数原型
示例
迭代器
begin,cbegin,rbegin,crbegin,end,cend,rend,crend
begin:返回一个指向映射中第一个元素或空映射后的位置的双向迭代器。
cbegin:返回一个范围或刚超出空范围末尾的位置中的第一个元素的const双向迭代器。
由于使用 cbegin
的返回值,因此不能修改范围中的元素。
rbegin:返回一个反向双向迭代器,用于寻址反向 map 中的第一个元素或寻址曾是非反向 map 中的最后一个元素的元素。
crbegin:返回一个常量迭代器,此迭代器用于寻址反向map 中的第一个元素。
end:返回超过末尾迭代器。
cend:返回刚超出范围中的最后一个元素的位置的const迭代器。
rend:返回一个迭代器,此迭代器用于寻址反向 map 中最后一个元素之后的位置。
crend:返回一个常量迭代器,此迭代器用于寻址反向映射中最后一个元素之后的位置。
函数原型
//begin
const_iterator begin() const;
iterator begin();
//cbegin
const_iterator cbegin() const;
//rbegin
const_reverse_iterator rbegin() const;
reverse_iterator rbegin();
//crbegin
const_reverse_iterator crbegin() const;
//end
const_iterator end() const;
iterator end();
//cend
const_iterator cend() const;
//rend
const_reverse_iterator rend() const;
reverse_iterator rend();
//crend
const_reverse_iterator crend() const;
示例
#include <map>
#include <iostream>
int main( )
{
using namespace std;
map <int, int> m1;
map <int, int> :: iterator m1_Iter;
map <int, int> :: const_iterator m1_cIter;
typedef pair <int, int> Int_Pair;
m1.insert ( Int_Pair ( 0, 0 ) );
m1.insert ( Int_Pair ( 1, 1 ) );
m1.insert ( Int_Pair ( 2, 4 ) );
m1_cIter = m1.begin ( );
cout << "The first element of m1 is " << m1_cIter -> first << endl;
m1_Iter = m1.begin ( );
m1.erase ( m1_Iter );
// The following 2 lines would err because the iterator is const
// m1_cIter = m1.begin ( );
// m1.erase ( m1_cIter );
m1_cIter = m1.cbegin( );
cout << "The first element of m1 is now " << m1_cIter -> first << endl;
m1_rIter = m1.rbegin( );
cout << "The first element of the reversed map m1 is "
<< m1_rIter -> first << "." << endl;
}
懒得写了,下面没有了
map::clear
函数原型
示例
map::count
函数原型
示例
map::emplace
函数原型
示例
map::emplace_hint
函数原型
示例
map::empty
函数原型
示例
map::equal_range
函数原型
示例
map::erase
函数原型
示例
map::find
函数原型
示例
map::get_allocator
函数原型
示例
map::insert
函数原型
示例
map::key_comp
函数原型
示例
map::lower_bound
函数原型
示例
map::max_size
函数原型
示例
map::size
函数原型
示例
map::swap
函数原型
示例
map::upper_bound
函数原型
示例
map::value_comp
函数原型
示例