<map>的基本用法

用于存储和检索集合中的数据,此集合中的每个元素均为包含数据值和排序键的元素对。 键的值是唯一的,用于自动排序数据。
map是stl的一种关联容器,它提供一对一的数据处理能力。
map内部的实现,自建一颗红黑树,具有对数据自动排序的功能。
可以直接更改映射中的元素值。 键值是常量,不能更改。 必须先删除与旧元素关联的键值,才能为新元素插入新键值

C++ 标准库 map 类为:
大小可变的关联容器,基于关联键值高效检索元素值。
可逆,因为它提供双向迭代器来访问其元素。
有序,因为它的元素根据指定的比较函数按键值排序。
唯一。 因为它的每个元素必须具有唯一键。
关联容器对,因为它的元素数据值与其键值不同。
类模板,因为它提供的功能是泛型的,独立于元素或键类型。 用于元素和键的数据类型作为类模板以及比较函数和分配器中的参数指定。

map 是以 pair形式插入的。
map中的元素的类型value_type

1-声明方式
map<int,string> mymap;

2-数据的插入
使用insert函数利用pair和value_type插入数据
mymap.insert(pair<int,string>(1,“Toms”))
mymap.insert(map<int,string>::value_type(1,“Toms”))

3:用数组的方式插入数据
map<int,string> mymap’
mymap[1]=“tom”;
mymap[2]=“jion”;
mymap[3]=“jack”;

map容器基本的定义,存储,查找

#include <map>
#include <iostream>

typedef std::map<char, int> Mymap;  //定义一个map类型
int main()
{
   
    Mymap c1;       //构造对象

    //向map容器中添加元素
    c1.insert(Mymap::value_type('a', 1));
    c1.insert(Mymap::value_type('b', 2));
    c1.insert(Mymap::value_type('c', 3));

    // find and show elements-> 使用at函数查找元素
    std::cout << "c1.at('a') == " << c1.at('a') << std::endl;
    std::cout << "c1.at('b') == " << c1.at('b') << std::endl;
    std::cout << "c1.at('c') == " << c1.at('c') << std::endl;

    return (0);
    /*
        输出结果
    
      c1.at('a') == 1
      c1.at('b') == 2
      c1.at('c') == 3
      
    */
}

上述代码中首先使用typedef类型别名来定义一个map容器,这么做是为了整体代码的可阅读性,简洁性。
并且实例insert函数使用value_type来为容器添加元素,注意括号内的域运算符,如果没有则会报错。
最后使用at()函数来查找元素,查找a键值,可得到对应的存储数据。

常量迭代器和pair插入元素



#include <map>
#include <iostream>

int main()
{
   
    using namespace std;
    map <int, int> m1;  //创建容器对象m1

    map <int, int> ::iterator m1_Iter;
    map <int, int> ::const_iterator m1_cIter;   //创建连个迭代器,一个为常量迭代器
    typedef pair <int, int> Int_Pair;           //定义类型别名,方便书写以及代码的简洁

    m1.insert(Int_Pair(1, 2));          //为容器添加元素三组
    m1.insert(Int_Pair(3, 4));
    m1.insert(Int_Pair(5, 6));

    m1_cIter = m1.begin();              //迭代器操作
    cout << "The first element of m1 is " << m1_cIter->first << endl;

    m1_Iter = m1.begin();       //将指向第一个元素的迭代器删除,即删除了第一个元素
    m1.erase(m1_Iter);

   

    m1_cIter = m1.begin();      //删除容器第一行元素之后重新赋值迭代器的第一个元素就是3
    cout << "The first element of m1 is now " << m1_cIter->first << endl;

   /* 
   运行结果:
        The first element of m1 is 1
        The first element of m1 is now 3
        
   */
   
}
}

上述代码中会发现,定义一个类型别名的pair会比定义一个map容器,并使用value_type插入元素数据,简洁许多。
并且上述代码介绍了常量迭代器的一些基本运用和删除方法。
迭代器的first/second意思是指向一组元素数据的键和值。

clear()函数,清空容器元素


#include <map>
#include <iostream>

int main()
{
   
    using namespace std;
    map<int, int> m1;
    map<int, int>::size_type i;
    typedef pair<int, int> Int_Pair;

    m1.insert(Int_Pair(1, 2));
    m1.insert(Int_Pair(3, 4));

    i = m1.size();
    cout << "The size of the map is initially "
        << i << "." << endl;

    m1.clear();         //使用clear()函数会清空容器内的所有元素。
    i = m1.size();
    cout << "The size of the map after clearing is "
        << i << "." << endl;
}

常量引用(对象绑定)


#include <map>
#include <iostream>

int main()
{
   
    using namespace std;
    map <int, int> m1;
    typedef pair <int, int> Int_Pair;

    m1.insert(Int_Pair(1, 10));
    m1.insert(Int_Pair(2, 20));

    // int &Ref1 = ( m1.begin( ) -> first );  ->这里必须指出,访问key值必须使用const限定符,
    //因为key值不可修改,为const
       const int& Ref1 = (m1.begin()->first);

    cout << "The key of first element in the map is "
        << Ref1 << "." << endl;

        int& Ref2 = (m1.begin()->second);  //而访问值可以不需要const限定符,因为值是可以修改的。

    cout << "The data value of first element in the map is "
        << Ref2 << "." << endl;
}

访问key需要const限定符,因为key不可修改,而访问值则不需要,因为值可以修改。

count()查找函数


#include <map>
#include <iostream>

int main()
{
   
    using namespace std;
    map<int, int> m1;
    map<int, int>::size_type i;
    typedef pair<int, int> Int_Pair;

    m1.insert(Int_Pair(1, 1));
    m1.insert(Int_Pair(2, 1));
    m1.insert(Int_Pair(1, 4));
    m1.insert(Int_Pair(2, 1));

    //返回值为1=true,则包含此元素,返回0=flase,则没有
    i = m1.count(1);    //查找元素int1
    cout << "The number of elements in m1 with a sort key of 1 is: "
        << i << "." << endl;

    i = m1.count(2);    //查找元素int2
    cout << "The number of elements in m1 with a sort key of 2 is: "
        << i << "." << endl;

    i = m1.count(3);    //查找元素int3
    cout << "The number of elements in m1 with a sort key of 3 is: "
        << i << "." << endl;


   /* 
    运算结果:
        The number of elements in m1 with a sort key of 1 is: 1.
        The number of elements in m1 with a sort key of 2 is : 1.
        The number of elements in m1 with a sort key of 3 is : 0.*/
}

这个函数比较简单,通过0/1来判断容器内是否存在需要查找的元素。

difference_type迭代器增减类型,用以遍历

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值