c++ STL map简单使用

 

map字典存放键值对

内部组成是红黑树  查找 删除 插入复杂度为O(logn)

 

初始化方式

map<int,string>  str;

 

插入方式

1.使用pair 

map<int ,string>str;
str.insert(pair<int, string>(1, "one"));  

2.value_type方式

map<int,string>str;
map.insert(map<int,string>::value_type(1,"one"));

3.数组方式

map<int,string>str;
str[1] = "one";

insert方式插入关键字存在,无法插入。

使用数组可以覆盖关键字的值

 

遍历时可使用反向迭代器遍历

map<int, string>::reverse_iterator iter;  
for(iter=str.rbegin();iter!=str.rend();++iter)
{
        cout<<iter->first<<iter->second<<endl;
}

 

str.count(1);判断是否存在此键值对

str.find(1) 返回此键值对迭代器位置

swap 是交换两个map容器;

原文链接: https://www.cnblogs.com/9527s/p/13199534.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    c++ STL map简单使用

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/359526

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年3月2日 下午12:48
下一篇 2023年3月2日 下午12:48

相关推荐