C++STL容器之map详解

 

总结

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_typemap对象的 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

函数原型

示例

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++ STL(Standard Template Library)是C++标准库中的一个重要组成部分,提供了一组数据结构和算法的模板类,可以大大简化C++程序的开发过程。STL包含了多个容器类,每个容器类都有其特定的特性和用途。 STL中的容器类主要分为序列容器和关联容器两大类。序列容器包括vector、list、deque和array,它们按照元素在容器中的位置进行存储和访问。关联容器包括set、multiset、map、multimap和unordered系列容器,它们按照键值进行存储和访问。 序列容器具有以下特性: 1. 动态大小:序列容器可以根据需要动态调整大小,可以在任意位置插入和删除元素。 2. 快速随机访问:序列容器中的元素可以通过索引快速访问,时间复杂度为O(1)。 3. 按顺序存储:序列容器中的元素按照插入的顺序存储,并保持元素的相对位置不变。 4. 支持迭代器:序列容器提供了迭代器,可以通过迭代器遍历容器中的元素。 关联容器具有以下特性: 1. 自动排序:关联容器中的元素按照键值自动排序,并且可以根据自定义的比较函数进行排序。 2. 快速查找:关联容器支持快速的查找操作,时间复杂度为O(log n)。 3. 不允许重复键值:set和map容器中的键值是唯一的,而multiset和multimap容器允许重复的键值。 4. 无序容器:unordered系列容器C++11引入的,它们使用哈希函数来存储和访问元素,查找操作的平均时间复杂度为O(1)。 总而言之,C++ STL提供了丰富的容器类,每个容器类都有其独特的特性和适用场景,可以根据具体需求选择合适的容器来存储和操作数据。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [C++——STL容器](https://blog.csdn.net/JAN6055/article/details/122758690)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [C++STL容器详解](https://blog.csdn.net/Jinyizhi2233/article/details/131640448)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值