关于Map的应用实例
2017-12-14 12:07
441 查看
#include"stdafx.h" #include<iostream> #include<string> #include<map> using namespace std; #if 0 map<string, int> m_map = { { "DBF", 1209 }, { "DAW", 1202 } }; //初始化赋值 typedef map<string, int>::iterator iter; int main() { //m_map m_map.insert(pair<string,int>("DF1", 1990)); //pair是一个模板类 m_map.insert(make_pair("DYW1", 1991)); //make_pair 返回的是一个pair对象 m_map.insert(map <string, int>::value_type("DG", 23)); m_map.insert(pair<string, int>("DD", 2990)); auto it = m_map.begin(); while (it != m_map.end()) { cout <<it->first<<"="<< it->second << endl; it++; } cout << "mapzie= " << m_map.size() << endl; /*判断插入的是成功*/ pair<iter, bool> inPair; inPair = m_map.insert(make_pair("DYW111", 1992)); if (inPair.second == true) { cout << "insert Suceed!" << endl; } else { cout << "inseret failed" << endl; } /*end*/ iter it2 = m_map.find("DBF"); if (it2 != m_map.end()) { cout << it2->second << endl; } iter it3 = m_map.lower_bound("DA"); //找到大于等于"DA"的迭代器 iter it4 = m_map.upper_bound("DF"); //找到大于"DF"的迭代器 /*删除map中多个数据*/ for (; it3 != it4; ) { cout << "Data" << it3->second << endl; iter itdel = it3; it3++; m_map.erase(itdel); it4 = m_map.upper_bound("DF"); } it = m_map.begin(); while (it != m_map.end()) { cout << it->first << "=" << it->second << endl; it++; } /*删除map中全部数据*/ m_map.erase(m_map.begin(), m_map.end()); cout << "mapzie= " << m_map.size() << endl; system("pause"); } #endif typedef struct MyStruct { int nID; string name; bool operator<( const MyStruct &stru) const { return nID < stru.nID ? true : false; } }StuInfo; map<StuInfo, int> m_stu; int main2() { StuInfo s1 = { 16, "DF" }; StuInfo s2 = { 13, "DYW" }; /*s1.name = "DF"; s1.nID = 12; s2.name = "DYW"; s2.nID = 13;*/ m_stu.insert(make_pair(s1, 300)); m_stu.insert(make_pair(s2, 200)); auto it = m_stu.begin(); while (it!= m_stu.end()) { cout << (it->first).name<< endl; it++; } cout << "hello<<endl" << endl; system("pause"); return 0; }
相关文章推荐
- list,set,map应用实例
- java中Map集合的常用遍历方法及HashMap的应用实例
- 关于c++中map的应用
- 关于继承的应用小实例
- Vue框架中关于vue-lazyload的简单应用实例(学习笔记③)
- 关于<%= str%>和<%# str %>两种方式的应用实例
- Google map api应用实例 2
- 关于Spring-JPA框架下使用多表查询的应用实例记录
- BGP advertise-map 应用实例
- java中Map集合的常用遍历方法及HashMap的应用实例
- 关于c#静态方法和实例方法的辨析和应用
- 关于tilemap在platform游戏中的应用
- Map 的遍历过程及应用实例
- 关于接口的基本应用实例讲解
- Problem A: STL—水果店(map见解及实例应用)
- Map Traversal 应用实例
- 关于C++ STL标准库中map 的多元素应用
- 关于C++中static_cast和reinterpret_cast的区别 以及实例应用
- 关于百度编辑器UEditor(1.4.3)在C#.NET中的应用实例
- java中Map集合的常用遍历方法及HashMap的应用实例