码迷,mamicode.com
首页 > 编程语言 > 详细

c++ STL map简单使用

时间:2020-06-27 20:10:09      阅读:73      评论:0      收藏:0      [点我收藏+]

标签:count   class   end   col   map容器   初始   str   红黑树   for   

 

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容器;

c++ STL map简单使用

标签:count   class   end   col   map容器   初始   str   红黑树   for   

原文地址:https://www.cnblogs.com/9527s/p/13199534.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!