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 ...
随机推荐
- Wpf从资源中重用UI元素
在我的界面上有几个选项卡,每个选项卡中都有下面的元素: <StackPanel Orientation="Horizontal"> <Button Content ...
- 16、WPF中的命令
一.前言 事件的作用是发布.传播一些信息,消息送达接收者,事件的使命就算完成了,至于如何响应事件送来的消息事件并不做规定,每个接收者可以使用自己的行为来响应事件,也就是说事件不具有约束力.命令能够在代 ...
- 在Linux下写一个简单的驱动程序
本文首先描述了一个可以实际测试运行的驱动实例,然后由此去讨论Linux下驱动模板的要素,以及Linux上应用程序到驱动的执行过程.相信这样由浅入深.由具体实例到抽象理论的描述更容易初学者入手Linux ...
- C#TCPClient应用-一个简单的消息发送和接收
TcpSend窗口用于发送消息,另外写一个用于接收消息的应用程序,消息接受到以后,必须要关闭接收消息的窗口,才能在接收新的消息,不知道怎么能解决这个问题. 源代码: 发送消息的窗口代码 using S ...
- matlab实现判断是否能否生成严格对角占优矩阵
如题: function X = IsStrictDiagMatrix(A) % input: A matrix % output: The matrix after transformation % ...
- matlab实现复合梯形法则
复合梯形法则: function int_f = CompoundEchelon( f, a, b, m ) % input : f : function handler % a : the lowe ...
- php配置步奏
web运行大致流程 浏览器输入地址,回车(发送请求) 根据规则找到对应web服务器.规则如下: 首先在本机hosts文件中找对应IP 如果hosts中没有找到,则到互联网上找对应IP 如果还 ...
- Oracle 异常处理
1.什么是异常 在PL/SQL中的一个警告或错误的情形都可被称为异常.包括编译时错误(PLS)和运行时错误(ORA).一个异常通常包含一个错误代码和错误文本,分别指示异常的编号和具体错误信息. 异 ...
- 屏蔽ios7中某个页面的默认手势滑回返回
- (void)viewWillDisappear:(BOOL)animated {[super viewWillDisappear:YES];self.navigationController.in ...
- duilib中各控件响应的消息类型
消息 说明 Sender click 鼠标点击 CButtonUI dropdown 下拉显示 CComboUI headerclick 点击列标题 CListHeaderItemUI itemact ...