c++ map 记录。为了自己记录理解
来源:互联网 发布:阿里云域名访问不了 编辑:程序博客网 时间:2024/05/21 00:49
容器map,存在形式为key—value键值对。实际就是pair类型。
map定义的类型:
map<K,V>::key_type 在map中用做索引的键的类型
map<K,V>::mapped_type 在map中键所关联的值的类型
map<K,V>::value_type 一个pair类型,它的first元素具有const map<K,V>::key_type 类型,而second元素则为map<K,V>::mapped_type类型
map迭代器进行解引用将产生pair类型的对象,如:
map<string,int>::iterator map_it = word_count.begin();
cout<<map_it->first<<""<<map_it->second<<endl;
使用Insert对map进行插入:
m .insert(e) e是一个用在m上的value_type类型的值,如果e.first不在m中则插入一个值为.second的新元素,否则该键值在m中已经存在则保持不变,该 函数返回一个pair新类型,包含一个指向键值为e.first的元素的map迭代器,以及一个bool类型的对象,表示是否插入该元素 例如:std::pair<std::map<自己要用的key和value类型>::iterator,bool>
m.insert(beg,end) 插入beg、end标记范围内的元素,如果该元素的m.first已经存在则不插入否则插入。返回void类型
m.insert(iter,e) 如果e.first不在m中,则创建新元素,并以迭代器iter为起点搜索新元素的存储位置,否则返回一个迭代器,指向m中具有的给定键的元素。
从map中删除元素 ,使用erase与顺序容器功能一样:
m.erase(k) 删除m中键为k的元素。返回值为被删除元素的个数,对于map容器而言,其值必然是0或1。
m.erase(p) 从m中删除迭代器p所指向的元素。返回值为void类型。
m.erase(b,e) 从m中删除一段由一对迭代器范围的元素。返回值为void类型。
练习:
#include <iostream>
#include <map>
using std::string;
using std::cout;
using std::endl;
void print_m(std::map<string,int> &obj)
{
typedef std::map<string,int> mp;
mp::iterator it;
cout<<"map:";
for (it = obj.begin();it != obj.end();it++)
cout<<it->first<<" "<<it->second<<endl;
cout<<endl;
}
int main(int argc,char *argv[])
{
std::map<string,int> m;
m.clear();
cout<<"m1.size():"<<m.size()<<" m1.empty():"<<m.empty()<<endl;
m["s1"] = 1;
m["s2"] = 2;
m["s1"] = 11;
std::map<string,int>::key_type key;
key = "p1";
std::map<string,int>::mapped_type key_v;
key_v = 3;
std::map<string,int>::value_type value(key,key_v);
std::pair<std::map<string,int>::iterator,bool> ret;
ret = m.insert(value);
cout<<"pair value_type insert() return:"<<ret.first->first<<" "<<ret.first->second<<" "<<ret.second<<endl;
ret = m.insert(std::pair<string,int>("p2",4));
cout<<"pair direct insert() return:"<<ret.first->first<<" "<<ret.first->second<<" "<<ret.second<<endl;
ret = m.insert(std::pair<string,int>("p2",4));
cout<<"pair repeat insert() return:"<<ret.first->first<<" "<<ret.first->second<<" "<<ret.second<<endl;
print_m(m);
m.insert(m.begin(),std::pair<string,int>("b3",5));
print_m(m);
m.insert(++m.begin(),std::pair<string,int>("b4",6));
print_m(m);
m.insert(m.end(),std::pair<string,int>("e3",7));
print_m(m);
m.insert(++m.end(),std::pair<string,int>("e4",8));
print_m(m);
m.insert(++m.end(),std::pair<string,int>("e4",9));
print_m(m);
cout<<"m1.size():"<<m.size()<<" m1.empty():"<<m.empty()<<endl;
m.erase(key);
print_m(m);
m.erase(++m.begin());
print_m(m);
return 0;
}
- c++ map 记录。为了自己记录理解
- C语言运算符优先级记录-为了自己查找方便
- 为了庆祝自己重生,记录一篇
- 为了记录
- Java Map《自己记录记忆》
- 对map的理解记录
- 开博客是为了记录自己的技术成长历程
- 这篇文章主要是为了整理 记录 c# 多线程 的理解
- 为了开始的记录
- 写博客不是为了分享和记录,只是为了自己总结
- 记录自己今天理解的 equals 方法
- 记录自己理解的一些设计模式
- 此Blog的作用:为了鼓励自己做事,为了记录自己做的事,为了回头时还记得自己做过的事.
- C++Primer记录自己不清楚的内容
- 自己c写的信息记录系统
- 只是为了 记录下密码。。。
- 隔了N年重新写博客,为了记录自己重新学习Html历程
- C语言中的指针-记录个人理解
- 理解C语言的数组和指针
- 使用AlertDialog报错 You need to use a Theme.AppCompat theme (or descendant) with this activity.之解决
- 简析java字节流和字符流
- iOS app各种图片尺寸(Icon,Launchimg,应用市场)
- 魔方阵原理及十种解法(C语言)
- c++ map 记录。为了自己记录理解
- java MP3等格式转换和压缩
- 并查集学习笔记1
- 完全解决!android studio更新 check for updates失败的问题
- android-UIL图片缓存框架 主要类包括的成员
- c语言之数组
- [微信开发利器]微信内移动前端开发抓包调试工具fiddler使用教程
- where()
- Codeblocks c++11 std::thread问题