map的详细用法
- map<int, string> mapStudent;
- map<int, string> mapStudent;
- #include <map>
- #include <string>
- #include <iostream>
- using namespace std;
- int main()
- {
- map<int, string> mapStudent;//pair<int,string>p;p=make_pair(v1,v2);<span style="color: rgb(255, 0, 0); background-color: rgb(240, 248, 255); font-family: Arial; font-size: 13px; "> </span>
- mapStudent.insert(pair<int, string>(1, "student_one"));
- mapStudent.insert(pair<int, string>(2, "student_two"));
- mapStudent.insert(pair<int, string>(3, "student_three"));
- map<int, string>::iterator iter;
- for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
- {
- cout<<iter->first<<" "<<iter->second<<endl;
- }
- }
- make_pair()//返回类型为对应的pair类型
- 无需写出类别,就可以生成一个pair对象
- 例:
- make_pair(1,'@')
- 而不必费力的写成
- pair<int ,char>(1,'@')
- #include <map>
- #include <string>
- #include <iostream>
- using namespace std;
- int main()
- {
- map<int, string> mapStudent;
- mapStudent.insert(map<int, string>::value_type (1, "student_one"));
- mapStudent.insert(map<int, string>::value_type (2, "student_two"));
- mapStudent.insert(map<int, string>::value_type (3, "student_three"));
- map<int, string>::iterator iter;
- for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
- {
- cout<<iter->first<<" "<<iter->second<<endl;
- }
- }
- #include <map>
- #include <string>
- #include <iostream>
- using namespace std;
- int main()
- {
- map<int, string> mapStudent;
- mapStudent[1] = "student_one";
- mapStudent[2] = "student_two";
- mapStudent[3] = "student_three";
- map<int, string>::iterator iter;
- for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
- {
- cout<<iter->first<<" "<<iter->second<<endl;
- }
- }
- mapStudent.insert(map<int, string>::value_type (1, "student_one"));
- mapStudent.insert(map<int, string>::value_type (1, "student_two"));
- Pair<map<int, string>::iterator, bool> Insert_Pair;
- Insert_Pair = mapStudent.insert(map<int, string>::value_type (1, "student_one"));
- #include <map>
- #include <string>
- #include <iostream>
- using namespace std;
- int main()
- {
- map<int, string> mapStudent;
- Pair<map<int, string>::iterator, bool> Insert_Pair;
- Insert_Pair = mapStudent.insert(pair<int, string>(1, "student_one"));
- If(Insert_Pair.second == true)
- {
- cout<<"Insert Successfully"<<endl;
- }
- Else
- {
- cout<<"Insert Failure"<<endl;
- }
- Insert_Pair = mapStudent.insert(pair<int, string>(1, "student_two"));
- If(Insert_Pair.second == true)
- {
- cout<<"Insert Successfully"<<endl;
- }
- Else
- {
- cout<<"Insert Failure"<<endl;
- }
- map<int, string>::iterator iter;
- for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
- {
- cout<<iter->first<<" "<<iter->second<<endl;
- }
- }
- #include <map>
- #include <string>
- #include <iostream>
- using namespace std;
- int main()
- {
- map<int, string> mapStudent;
- mapStudent[1] = "student_one";
- mapStudent[1] = "student_two";
- mapStudent[2] = "student_three";
- map<int, string>::iterator iter;
- for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
- {
- cout<<iter->first<<" "<<iter->second<<endl;
- }
- }
- int nSize = mapStudent.size();
- #include <map>
- #include <string>
- #include <iostream>
- using namespace std;
- int main()
- {
- map<int, string> mapStudent;
- mapStudent.insert(pair<int, string>(1, "student_one"));
- mapStudent.insert(pair<int, string>(2, "student_two"));
- mapStudent.insert(pair<int, string>(3, "student_three"));
- map<int, string>::reverse_iterator iter;
- for(iter = mapStudent.rbegin(); iter != mapStudent.rend(); iter++)
- {
- cout<<iter->first<<" "<<iter->second<<endl;
- }
- }
- #include <map>
- #include <string>
- #include <iostream>
- using namespace std;
- int main()
- {
- map<int, string> mapStudent;
- mapStudent.insert(pair<int, string>(1, "student_one"));
- mapStudent.insert(pair<int, string>(2, "student_two"));
- mapStudent.insert(pair<int, string>(3, "student_three"));
- int nSize = mapStudent.size()
- //此处有误,应该是 for(int nIndex = 1; nIndex <= nSize; nIndex++)
- //by rainfish
- for(int nIndex = 0; nIndex < nSize; nIndex++)
- {
- cout<<mapStudent[nIndex]<<end;
- }
- }
- #include <map>
- #include <string>
- #include <iostream>
- using namespace std;
- int main()
- {
- map<int, string> mapStudent;
- mapStudent.insert(pair<int, string>(1, "student_one"));
- mapStudent.insert(pair<int, string>(2, "student_two"));
- mapStudent.insert(pair<int, string>(3, "student_three"));
- map<int, string>::iterator iter;
- iter = mapStudent.find(1);
- if(iter != mapStudent.end())
- {
- cout<<"Find, the value is "<<iter->second<<endl;
- }
- Else
- {
- cout<<"Do not Find"<<endl;
- }
- }
- #include <map>
- #include <string>
- #include <iostream>
- using namespace std;
- int main()
- {
- map<int, string> mapStudent;
- mapStudent[1] = "student_one";
- mapStudent[3] = "student_three";
- mapStudent[5] = "student_five";
- map<int, string>::iterator iter;
- iter = mapStudent.lower_bound(2);
- {
- //返回的是下界3的迭代器
- cout<<iter->second<<endl;
- }
- iter = mapStudent.lower_bound(3);
- {
- //返回的是下界3的迭代器
- cout<<iter->second<<endl;
- }
- iter = mapStudent.upper_bound(2);
- {
- //返回的是上界3的迭代器
- cout<<iter->second<<endl;
- }
- iter = mapStudent.upper_bound(3);
- {
- //返回的是上界5的迭代器
- cout<<iter->second<<endl;
- }
- Pair<map<int, string>::iterator, map<int, string>::iterator> mapPair;
- mapPair = mapStudent.equal_range(2);
- if(mapPair.first == mapPair.second)
- {
- cout<<"Do not Find"<<endl;
- }
- Else
- {
- cout<<"Find"<<endl;
- }
- mapPair = mapStudent.equal_range(3);
- if(mapPair.first == mapPair.second)
- {
- cout<<"Do not Find"<<endl;
- }
- Else
- {
- cout<<"Find"<<endl;
- }
- }
- #include <map>
- #include <string>
- #include <iostream>
- using namespace std;
- int main()
- {
- map<int, string> mapStudent;
- mapStudent.insert(pair<int, string>(1, "student_one"));
- mapStudent.insert(pair<int, string>(2, "student_two"));
- mapStudent.insert(pair<int, string>(3, "student_three"));
- //如果你要演示输出效果,请选择以下的一种,你看到的效果会比较好
- //如果要删除1,用迭代器删除
- map<int, string>::iterator iter;
- iter = mapStudent.find(1);
- mapStudent.erase(iter);
- //如果要删除1,用关键字删除
- int n = mapStudent.erase(1);//如果删除了会返回1,否则返回0
- //用迭代器,成片的删除
- //一下代码把整个map清空
- mapStudent.earse(mapStudent.begin(), mapStudent.end());
- //成片删除要注意的是,也是STL的特性,删除区间是一个前闭后开的集合
- //自个加上遍历代码,打印输出吧
- }
- #include <map>
- #include <string>
- uing namespace std;
- Typedef struct tagStudentInfo
- {
- int nID;
- String strName;
- }StudentInfo, *PStudentInfo; //学生信息
- int main()
- {
- int nSize;
- //用学生信息映射分数
- map<StudentInfo, int>mapStudent;
- map<StudentInfo, int>::iterator iter;
- StudentInfo studentInfo;
- studentInfo.nID = 1;
- studentInfo.strName = "student_one"
- mapStudent.insert(pair<StudentInfo, int>(studentInfo, 90));
- studentInfo.nID = 2;
- studentInfo.strName = "student_two";
- mapStudent.insert(pair<StudentInfo, int>(studentInfo, 80));
- for (iter=mapStudent.begin(); iter!=mapStudent.end(); iter++)
- cout<<iter->first.nID<<endl<<iter->first.strName<<endl<<iter->second<<endl;
- }
- Typedef struct tagStudentInfo
- {
- int nID;
- String strName;
- Bool operator < (tagStudentInfo const& _A) const
- {
- //这个函数指定排序策略,按nID排序,如果nID相等的话,按strName排序
- If(nID < _A.nID) return true;
- If(nID == _A.nID) return strName.compare(_A.strName) < 0;
- Return false;
- }
- }StudentInfo, *PStudentInfo; //学生信息
- #include <map>
- #include <string>
- using namespace std;
- Typedef struct tagStudentInfo
- {
- int nID;
- String strName;
- }StudentInfo, *PStudentInfo; //学生信息
- class sort
- {
- Public:
- Bool operator() (StudentInfo const &_A, StudentInfo const &_B) const
- {
- If(_A.nID < _B.nID) return true;
- If(_A.nID == _B.nID) return _A.strName.compare(_B.strName) < 0;
- Return false;
- }
- };
- int main()
- {
- //用学生信息映射分数
- map<StudentInfo, int, sort>mapStudent;
- StudentInfo studentInfo;
- studentInfo.nID = 1;
- studentInfo.strName = "student_one";
- mapStudent.insert(pair<StudentInfo, int>(studentInfo, 90));
- studentInfo.nID = 2;
- studentInfo.strName = "student_two";
- mapStudent.insert(pair<StudentInfo, int>(studentInfo, 80));
- }
map的详细用法的更多相关文章
- map的详细用法 (转
map的详细用法: map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能 力,由于这个特性,它完成有可能在我 ...
- Java中stream的详细用法
来自于:Java 8 stream的详细用法_旅行者-CSDN博客_java stream 一.概述 Stream 是 Java8 中处理集合的关键抽象概念,它可以指定你希望对集合进行的操作,可以执行 ...
- C#播放声音的四种方法 +AxWindowsMediaPlayer的详细用法
C#播放声音的四种方法 第一种是利用DirectX 1.安装了DirectX SDK(有9个DLL文件).这里我们只用到MicroSoft.DirectX.dll和 Microsoft.Directx ...
- 在DOS下的DEBUG命令的详细用法
在DOS下的DEBUG命令的详细用法 名称 解释 格式 a (Assemble) 逐行汇编 a [address] c (Compare) 比较两内存块 c range address d (Dump ...
- __declspec关键字详细用法
__declspec关键字详细用法 __declspec用于指定所给定类型的实例的与Microsoft相关的存储方式.其它的有关存储方式的修饰符如static与extern等是C和C++语言的ANSI ...
- CString.Format的详细用法(转)
CString.Format的详细用法(转) 在MFC程序中,使用CString来处理字符串是一个很不错的选择.CString既可以处理Unicode标准的字符串,也可以处理ANSI标准的字符串.CS ...
- HDU1004 Let the Balloon Rise(map的简单用法)
Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- IFRAM的详细用法
IFRAM的详细用法: IFRAM的详细用法: <IFRAME>用于设置文本或图形的浮动图文框或容器. BORDER <IFRAME BORDER="3"& ...
- 【转】java.util.vector中的vector的详细用法
[转]java.util.vector中的vector的详细用法 ArrayList会比Vector快,他是非同步的,如果设计涉及到多线程,还是用Vector比较好一些 import java.uti ...
随机推荐
- Oracle中Left join的on和where的效率差别
假设有两个表a.b 使用on Select * from a left join b on b.col = a.col and b.col2 = ‘aa’ 使用 where Select * from ...
- WPF 实现QQ抖动
//wpf中实现类似于qq的抖动窗效果 //前段页面 <Window x:Class="WpfApplication4.MainWindow" xmlns="htt ...
- COUNT(*),count(1),COUNT(ALL expression),COUNT(DISTINCT expression)
创建一个测试表 IF OBJECT_ID( 'dbo.T1' , 'U' )IS NOT NULL BEGIN DROP TABLE dbo.T1; END; GO )); GO INSERT INT ...
- MySQL通过Binlog恢复删除的表
查看log-bin是否开启:mysql> show variables like '%log%bin%';+---------------------------------+-------+| ...
- linux总线、设备和设备驱动的关系
之一:bus_type 总线是处理器和一个或多个设备之间的通道,在设备模型中,所有的设备都通过总线相连,甚至是内部的虚拟"platform"总线.可以通过ls -l /sys/bu ...
- Access
一般系统的实现: 管理系统的分析与设计 --->>数据表的设计创建 --->> 设计“查询”与“宏” --->> 创建窗体与报表 --->>系统注册 启 ...
- Running Central Admin on Multiple Servers within a Farm
http://sharepoint.microsoft.com/blogs/fromthefield/Lists/Posts/Post.aspx?ID=60
- SlimDx绘制点图元的问题
问题:点图元在自己创建的三维环境里渲染不出来,代码如下: GMCustomVertex.PositionNormalColored wellPart = new GMCustomVertex.Posi ...
- python学习小结4:类
虽然Python是解释性语言,但是它是面向对象的,能够进行对象编程. 类和对象是面向对象编程的两个主要方面.类:创建一个新类型,而对象是这个类的实例,类使用class关键字创建.类的域和方法被列在一个 ...
- AvalonDock 2.0+Caliburn.Micro+MahApps.Metro实现Metro风格插件式系统(菜单篇)
这章主要说插件的菜单,可以说菜单是最核心的部分,前面我们已经实现了Document添加,现在主要就是生成具有层级关系的菜单,以及把菜单跟我们自定义的Document关联起来,也就是MenuPart-& ...