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 ...
随机推荐
- jquery插件formValidator的ajaxValidator传参数问题
最近在用formValidator插件,遇到一个问题.当我想用ajaxValidator的url传参数时,$("#tbName").val().document.getElemen ...
- HIVE中join、semi join、outer join举例详解
转自 http://www.cnblogs.com/xd502djj/archive/2013/01/18/2866662.html 举例子: hive> select * from zz0; ...
- @RenderSection与@RenderBody
_LayoutMain: <html> <head> @RenderSection("head") </head> <body> @ ...
- WPF中实现Button.Content变化的简易动画
项目中曾要这样的需求——输入法的切换,要求从English切换到简体中文的时候,Button的Content先从English变成空白,再从空白变成简体中文, 而不是直接从English变成简体中文. ...
- Python数据结构——链表的实现
链表由一系列不必在内存中相连的结构构成,这些对象按线性顺序排序.每个结构含有表元素和指向后继元素的指针.最后一个单元的指针指向NULL.为了方便链表的删除与插入操作,可以为链表添加一个表头. 删除操作 ...
- Ztack学习笔记(2)-系统初始化分析
main函数先执行初始化工作,包括硬件.网络层.任务等的初始化. 一 系统初始化 系统初始化函数主要完成内存分配.消息队列头.定时器.电源管理.任务系统及内存栈等的初始化,具体如下代码所示: //os ...
- Storm集群安装详解
storm有两种操作模式: 本地模式和远程模式. 本地模式:你可以在你的本地机器上开发测试你的topology, 一切都在你的本地机器上模拟出来; 远端模式:你提交的topology会在一个集群的机器 ...
- UnitTest
using Bll; using Model; using Dal; using NUnit.Framework; using NUnit.Mocks; using System.ServiceMod ...
- 增强的PuTTY 以及 自定义主题
PuTTY很早之前就没有更新了(0.62),因为都是开源的所以有人branch出来做了增强,如这个PuTTY tray,增加了超链等功能: https://puttytray.goeswhere.co ...
- Android开发——AsyncTask详解
android提供AsynvTask,目的是为了不阻塞主线程(UI线程),且UI的更新只能在主线程中完成,因此异步处理是不可避免的. Android为了降低开发难度,提供了AsyncTask.Asyn ...