美文网首页程序员
标准模板库(STL)之 map 用法【初级】

标准模板库(STL)之 map 用法【初级】

作者: Aliven888 | 来源:发表于2020-08-20 11:24 被阅读0次

文档声明:
以下资料均属于本人在学习过程中产出的学习笔记,如果错误或者遗漏之处,请多多指正。并且该文档在后期会随着学习的深入不断补充完善。


资料仅供学习交流使用。
作者:Aliven888

1、简述

a. 映射通常实现为二进制搜索树。
b. 映射是关联的容器,存储按特定顺序由键值和映射值的组合形成的元素。
c. 可以使用方括号运算符((operator [])通过其对应的键直接访问映射中的映射值
d. 在映射中,键值通常用于对元素进行排序和唯一标识,而映射值存储与该键关联的内容。
e. 键和映射值的类型可能有所不同,并以成员类型value_type分组在一起,成员类型value_type是将两者结合的对类型。

2、接口函数

2.1、迭代器(Iterators)

名称 描述
begin Return iterator to beginning (public member function )
end Return iterator to end (public member function )
rbegin Return reverse iterator to reverse beginning (public member function)
rend Return reverse iterator to reverse end (public member function)
cbegin Return const_iterator to beginning (public member function)
cend Return const_iterator to end (public member function)
crbegin Return const_reverse_iterator to reverse beginning (public member function)
crend Return const_reverse_iterator to reverse end (public member function)

2.2、容量(Capacity)

名称 描述
empty Test whether container is empty (public member function)
size Return container size (public member function)
max_size Return maximum size (public member function)

2.3、元素访问(Element access)

名称 描述
operator[] Access element (public member function)
at Access element (public member function)

2.4、操作(Operations)

名称 描述
find Get iterator to element (public member function)
count Count elements with a specific key (public member function)
lower_bound Return iterator to lower bound (public member function)
upper_bound Return iterator to upper bound (public member function)
equal_range Get range of equal elements (public member function)

2.5、修改(Modifiers)

名称 描述
insert Insert elements (public member function)
erase Erase elements (public member function)
swap Swap content (public member function)
clear Clear content (public member function)
emplace Construct and insert element (public member function)
emplace_hint Construct and insert element with hint (public member function)

3、接口函数使用演示

3.1、定义一个变量:

    std::map<int, std::string> m_mapValue;
    //赋值
    m_mapValue.insert(pair<int, std::string>(1, "1"));
    m_mapValue.insert(pair<int, std::string>(2, "2"));
    m_mapValue.insert(pair<int, std::string>(3, "3"));

3.2、迭代器(Iterators)

    //获取映射表的起点元素所对应的迭代器 和 映射表的std::map::end(末尾元素的下一个)
    std::map<int, std::string>::iterator itBegin = m_mapValue.begin();
    std::map<int, std::string>::iterator itEnd = m_mapValue.end();
    
    //获取映射表的反向起点元素对应的迭代器 和 反向终点的元素对应的迭代器
    std::map<int, std::string>::reverse_iterator itRBegin = m_mapValue.rbegin();
    std::map<int, std::string>::reverse_iterator itREnd = m_mapValue.rend();

3.3、容量(Capacity)

    //判断映射表是否为空
    if (!m_mapValue.empty())
    {
        qDebug("m_mapValue is not empty.");
    }
    
    //获取映射表中当前存在的映射关系的个数
    int iSize = m_mapValue.size();
    
    //获取映射表所能容纳的最大映射关系个数,很据编译环境的不同,该值并一不定能达到最大。
    int iMax_size = m_mapValue.max_size();
    qDebug("m_mapValue size[%d] and max_size[%d].", iSize, iMax_size);

3.4、访问元素(Element access)

    //运算符重载 operator[]
    m_mapValue[4] = "4"; //赋值
    std::string strValue = m_mapValue[1]; //取值
    qDebug("m_mapValue size=[%d], strValue[%s]", m_mapValue.size(), strValue.c_str());
    
    //在 C++11 中引入的 at() 函数
    //根据 key 值修改与之对应的 value 值,如果 key 不存在,则返回 out_of_range
    m_mapValue.at(3) = "333"; //修改值
    //m_mapValue.at(5) = "5";   //建议不要这样使用,强行修改不存在的值,此时会导致程序会异常终止

3.5、操作(Operations)

    //find
    //找到某个key键指向的元素的迭代器
    std::map<int, std::string>::iterator itFind = m_mapValue.find(3);
    qDebug("itFind->first[%d], itFind->second[%s]", itFind->first, itFind->second.c_str());
    
    //count
    //元素计数,按照key键查找,因为map中key键都是唯一的,所以如果存在该函数返回1,否则返回0
    int iNum = m_mapValue.count(2);
    qDebug("m_mapValue has [%d] key[2]", iNum);
    
    //lower_bound
    //获取当前key的指向的迭代器
    //接口函数有:
    //iterator lower_bound(const key_type& k);
    //const_iterator lower_bound(const key_type& k) const;
    std::map<int, std::string>::iterator itLow = m_mapValue.lower_bound(2);
    
    //upper_bound
    //获取当前key指向元素的下一个元素的迭代器;如果是最后一个元素的key,则指向std::map:end
    //接口函数有:
    //iterator upper_bound(const key_type& k);
    //const_iterator upper_bound(const key_type& k) const;
    std::map<int, std::string>::iterator itUpper = m_mapValue.upper_bound(4);
    
    //equal_range
    //获取key键为2的元素的范围内元素,但是因为map中的key不允许重复,所以返回的两个迭代器
    //一个指向key键为2的元素,一个指向其后的元素,如果其后没有元素则指向std::map::end
    //pair<const_iterator,const_iterator>  equal_range (const key_type& k) const;
    //pair<iterator, iterator>             equal_range(const key_type& k);
    std::pair<std::map<int, std::string>::iterator, std::map<int, std::string>::iterator> itRet;
    itRet = m_mapValue.equal_range(2);
    

3.6、修改(Modifiers)

    //insert
    //插入元素到映射表中
    //常见接口函数
    //pair<iterator, bool> insert(const value_type& val);  //插入单个元素
    //iterator insert(iterator position, const value_type& val);  //在迭代器指向的元素前添加元素,但是因为map存在自动排序,所以插入的新的映射关系[key, value]要根据key键来决定
    //template <class InputIterator>  //按照范围添加
    //void insert(InputIterator first, InputIterator last);
    
    m_mapValue.insert(pair<int, std::string>(6, "6"));
    m_mapValue.insert(itBegin, pair<int, std::string>(0, "0"));
    
    std::map<int, std::string> mapChild;
    mapChild.insert(pair<int, std::string>(7, "7"));
    mapChild.insert(pair<int, std::string>(8, "8"));
    mapChild.insert(pair<int, std::string>(9, "9"));
    std::map<int, std::string>::iterator itChildBegin = mapChild.begin();
    std::map<int, std::string>::iterator itChildEnd = mapChild.end();
    m_mapValue.insert(itChildBegin, itChildEnd);
    
    //erase
    //清除某个元素或者某个范围内的元素
    //(1) void erase(iterator position);   清除迭代器指向的元素
    //(2) size_type erase(const key_type& k);  清除某个key值为k的元素
    //(3) void erase(iterator first, iterator last);  清除范围内的元素值
    itBegin = m_mapValue.begin();  //刷新迭代器
    m_mapValue.erase(itBegin);
    
    m_mapValue.erase(6);
    
    //清除 key 值在 [7 - 9] 之间的元素
    itBegin = m_mapValue.find(7);
    itEnd = m_mapValue.find(9);
    //itEnd = m_mapValue.end();
    m_mapValue.erase(itBegin, itEnd);
    
    //swap
    //交换元素类型相同的两个映射表的值
    m_mapValue.swap(mapChild);
    
    //emplace  C++11
    //向map中插入一对映射关系<key, value>, 并返回该映射关系指向的迭代器,如何改key键已存在,bool值返回false,否则返回true.
    //接口函数:
    //template <class... Args>
    //pair<iterator, bool> emplace(Args&&... args);
    std::pair<std::map<int, std::string>::iterator, bool> itNoExit = m_mapValue.emplace(2, "222");
    std::pair<std::map<int, std::string>::iterator, bool> itExit = m_mapValue.emplace(7, "777"); //此时的映射关系依旧是[7, "7"]
    
    //emplace_hint C++11
    //在迭代器的前面插入映射关系[key, value], 但是因为map存在自动排序,所以具体的位置有key键决定
    //返回的迭代器指向新加入的映射关系
    //接口函数:
    //template <class... Args>
    //iterator emplace_hint(const_iterator position, Args&&... args);
    itBegin = m_mapValue.begin();
    std::map<int, std::string>::iterator itNoExit_hit = m_mapValue.emplace_hint(itBegin, 3, "333");
    
    //clear
    //清空缓存
    m_mapValue.clear();
    

相关文章

网友评论

    本文标题:标准模板库(STL)之 map 用法【初级】

    本文链接:https://www.haomeiwen.com/subject/tpzkjktx.html