C++ Map基本操作
来源:互联网 发布:黎明杀机淘宝好便宜 编辑:程序博客网 时间:2024/05/24 05:34
下面举例说明什么是一对一的数据映射。比如一个班级中,每个学生的学号跟他的姓名就存在着一一映射的关系,这个模型用map可能轻易描述,很明显学号用int描述,姓名用字符串描述(本篇文章中不用char *来描述字符串,而是采用STL中string来描述),下面给出map描述代码:
#pragma warning (disable:4786)#include <map>#include <string>#include <iostream>using namespace std;void print(map<int,string> &maps){map<int,string>::iterator iter;cout<<endl;for(iter=maps.begin(); iter!=maps.end(); iter++){cout<<iter->first<<" "<<iter->second<<endl;}cout<<endl;}int main(){map<int,string> mapStudent;mapStudent[1]="student_zero"; ///1mapStudent[2]="student_two";mapStudent[1]="student_one";mapStudent.insert(pair<int,string>(3,"student_three")); ///2mapStudent.insert(pair<int,string>(4,"student_four"));mapStudent.insert(map<int,string>::value_type(5,"student_five")); ///3mapStudent.insert(map<int,string>::value_type(6,"student_six")); print(mapStudent);pair<map<int,string>::iterator,bool> insert_pair;insert_pair=mapStudent.insert(pair<int,string>(6,"student_six_2"));if(insert_pair.second==true)cout<<"insert 6 successfully"<<endl;elsecout<<"insert 6 failed"<<endl;insert_pair=mapStudent.insert(pair<int,string>(7,"student_seven"));if(insert_pair.second==true)cout<<"insert 7 successfully"<<endl;elsecout<<"insert 7 failed"<<endl;print(mapStudent);////数据的查找map<int,string>::iterator it;it=mapStudent.find(3);if(it!=mapStudent.end())cout<<"find,the value is "<<it->second<<endl;///删除元素it=mapStudent.find(3);if(it!=mapStudent.end()){it=mapStudent.erase(it); ///返回删除元素的下一个节点if(it!=mapStudent.end()){cout<<it->first<<" "<<it->second<<endl;}}int ret=mapStudent.erase(3);cout<<"ret: "<<ret<<endl;ret=mapStudent.erase(4);cout<<"ret: "<<ret<<endl;print(mapStudent);mapStudent.erase(mapStudent.begin(),mapStudent.end());return 0;}
输出为:
1 student_one
2 student_two
3 student_three
4 student_four
5 student_five
6 student_six
insert 6 failed
insert 7 successfully
1 student_one
2 student_two
3 student_three
4 student_four
5 student_five
6 student_six
7 student_seven
find,the value is student_three
4 student_four
ret: 0
ret: 1
1 student_one
2 student_two
5 student_five
6 student_six
7 student_seven
以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种和第二种在效果上是完成一样的,用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值,用程序说明
mapStudent.insert(map<int, string>::value_type (1, “student_one”));
mapStudent.insert(map<int, string>::value_type (1, “student_two”));
上面这两条语句执行后,map中1这个关键字对应的值是“student_one”,第二条语句并没有生效,那么这就涉及到我们怎么知道insert语句是否插入成功的问题了,可以用pair来获得是否插入成功,程序如下
Pair<map<int, string>::iterator, bool> Insert_Pair;
Insert_Pair = mapStudent.insert(map<int, string>::value_type (1, “student_one”));
我们通过pair的第二个变量来知道是否插入成功,它的第一个变量返回的是一个map的迭代器,如果插入成功的话Insert_Pair.second应该是true的,否则为false。
转自:http://www.yitsoft.com/chap_study/ch_00029/ch_00029.asp
- c++map基本操作
- C++map的基本操作和使用
- C++map的基本操作和使用
- C++map的基本操作和使用
- C++map的基本操作和使用
- C++map的基本操作和使用
- c++map的基本操作和用法
- C++map的基本操作和使用
- C++map的基本操作和使用
- C++map的基本操作和使用
- C++map的基本操作和使用
- C++map的基本操作和使用
- C++map的基本操作和使用
- C++map的基本操作和使用
- C++map的基本操作和使用
- c++map的基本操作跟使用
- C++map的基本操作和使用
- C++map的基本操作和使用
- leetCode 之 Subsets
- Android 漏洞提升权利的
- 使用Beaglebone Black的I2C(一)
- Django 数据关系的处理
- Android Fragment应用实战:点击底部栏不懂选项弹不同Fragment实例
- C++ Map基本操作
- 让学习成为习惯,将分享变成快乐
- MyBatis模糊查询的三种拼接方式
- 结构化异常处理SEH之__finally终止处理
- android 简单易用的ListView 实现多选的解决方案
- Android解析带转义符的JSON
- LeetCode (1) Two Sum (15)Three sum (16)Three sum closest (17)Four sum
- 迷茫的日子
- cmd命令大全/cmd命令提示符大全