//                        map使用
1 #include <iostream>
#include "insertVal.h"
#include "sort.h"
using namespace std; void main()
{
////////////三种插入方式////////////
InsertVal insert;
insert.insert_1();
cout<<"-------------------"<<endl;
insert.insert_2();
cout<<"-------------------"<<endl;
insert.insert_3();
cout<<"-------------------"<<endl;
/*以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,
当然了第一种和第二种在效果上是完全一样的,用insert函数插入数据,
在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,
insert操作是插入不了数据的,但是用数组方式就不同了,它可以覆盖以
前该关键字对应的值。*/ //////////map的大小//////////
map<int, int> iMap;
iMap[] = ;
iMap[] = ;
iMap[] = ;
cout<<iMap.size()<<endl; //////////map的遍历//////////
//
map<int, int>::reverse_iterator riter;
for(riter = iMap.rbegin(); riter != iMap.rend(); ++riter)//这里不要用后加,因为iterator是类模板,后加要返回一个无用的临时对象,
                                        //而it++是函数重载,所以编译器无法对其进行优化,所以每遍历一个元素,你就创建并销毁了一个无用的临时对象。
{
cout<<riter->first<<" "<<riter->second<<endl;
}
//
map<int, int>::iterator iter;
for(iter = iMap.begin(); iter != iMap.end(); ++iter)
{
cout<<iter->first<<" "<<iter->second<<endl;
}
//
map<string, int> sMap;
sMap["age"] = ;
sMap["height"] = ;
sMap["weight"] = ;
string str[] = {"age", "height", "weight"};
for (int i=; i<sMap.size(); i++)
{
auto val = sMap.at(str[i]);
cout<<str[i]<<":"<<val<<endl;
} //用count函数来判定关键字是否出现,结果返回1/0
cout<<sMap.count("age")<<endl; //用find函数来定位数据出现位置,它返回的一个迭代器,
//当数据出现时,它返回数据所在位置的迭代器,如果map
//中没有要查找的数据,它返回的迭代器等于end函数返回的迭代器
auto iters = sMap.find("age");
if(iters != sMap.end())
cout<<iters->first<<":"<<iters->second<<endl;
else
cout<<"no find!"<<endl; //Lower_bound函数用法,这个函数用来返回要查找关键字的下界(是一个迭代器)
//Upper_bound函数用法,这个函数用来返回要查找关键字的上界(是一个迭代器)
//例如:map中已经插入了1,2,3,4的话,如果lower_bound(2)的话,返回的2,
//而upper-bound(2)的话,返回的就是3
//Equal_range函数返回一个pair,pair里面第一个变量是Lower_bound返回的迭代器,
//pair里面第二个迭代器是Upper_bound返回的迭代器,如果这两个迭代器相等的话,
//则说明map中不出现这个关键字,
auto iter_1 = sMap.lower_bound("b");
auto iter_2 = sMap.upper_bound("b");
auto iter_pair = sMap.equal_range("ab"); /////////erase
sMap.erase("age");
sMap.erase(sMap.find("height"));
sMap.erase(sMap.begin(), --sMap.end());//删除一个区域是前闭后开的区间 /////////
if(sMap.empty())
sMap.clear(); sortShow();
return;
}

其中insertVal.h:

 #ifndef INSERTVAL_H
#define INSERTVAL_H #include <iostream>
#include <map>
#include <string>
using namespace std; class InsertVal
{
public:
void insert_1();
void insert_2();
void insert_3();
}; void InsertVal::insert_1()
{
map<int, string> mapStudent;
mapStudent.insert(pair<int, string>(, "Mical"));
mapStudent.insert(pair<int, string>(, "Meria"));
mapStudent.insert(pair<int, string>(, "Sam"));
map<int, string>::iterator iter;
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
{
cout<<iter->first<<"号学生是:"<<iter->second<<endl;
}
} void InsertVal::insert_2()
{
map<int, string> mapStudent;
mapStudent.insert(map<int, string>::value_type (, "Mical"));
mapStudent.insert(map<int, string>::value_type (, "Meria"));
mapStudent.insert(map<int, string>::value_type (, "Sam"));
map<int, string>::iterator iter;
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
{
cout<<iter->first<<"号学生是:"<<iter->second<<endl;
}
} void InsertVal::insert_3()
{
map<int, string> mapStudent;
mapStudent[] = "Mical";
mapStudent[] = "Meria";
mapStudent[] = "Sam";
map<int, string>::iterator iter;
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
{
cout<<iter->first<<"号学生是:"<<iter->second<<endl;
}
} #endif

sort.h:

#ifndef SORT_H
#define SORT_H
#include <iostream>
#include <map>
#include <string>
using namespace std;
/*
map内部自建一颗红黑树(一种非严格意义上的平衡二叉树),这颗树具有对数据自动排序的功能,所以在map内部所有的数据都是有序的
STL中默认是采用小于号来排序,
在一些特殊情况,比如关键字是一个结构体,涉及到排序就会出现问题,
因为它没有小于号操作,insert等函数在编译的时候过不去
方法一:重载小于号。略
方法二:仿函数的应用,这个时候结构体中没有直接的小于号重载。如下
*/ 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) < ;
return false;
}
}; void sortShow()
{
//用学生信息映射分数
map<StudentInfo, int, sort>mapStudent;
StudentInfo studentInfo;
studentInfo.nID = ;
studentInfo.strName = "student_one";
mapStudent.insert(pair<StudentInfo, int>(studentInfo, ));
studentInfo.nID = ;
studentInfo.strName = "student_two";
mapStudent.insert(pair<StudentInfo, int>(studentInfo, ));
} #endif

最新文章

  1. docker 1.8+之后ubuntu安装指定版本docker-engine
  2. Android_相关路径
  3. 【BZOJ】【1045/1465】【HAOI2008】糖果传递
  4. (转)Yale CAS + .net Client 实现 SSO(2)
  5. iOS开发——图形编程OC篇&amp;OpenGL ES2.0编程步骤
  6. SQL&nbsp;Server&nbsp;2008&nbsp;R2&nbsp;跟踪标志
  7. mysql优化---第7篇:参数 innodb_buffer_pool_instances设置
  8. rsync工作机制(翻译)
  9. http长连接与短连接
  10. ideal中把项目打成war包,并放在tomcat运行,遇见的问题。。。
  11. 以编程方式使用 Microsoft Office Visio 2003 ActiveX 控件
  12. linux 定时任务到秒级
  13. Spring Boot + Mybatis 实现动态数据源
  14. 《Linux内核分析》第八周学习笔记
  15. FCC JS基础算法题(7):Chunky Monkey(分割数组)
  16. secureCRT 设置字体时,显示字体较少问题
  17. BZOJ3738 [Ontak2013]Kapitał 【扩展Lucas】
  18. NoSQL非结构化数据库高级培训课程-大纲
  19. If a cache file exists, it is sent directly to the browser, bypassing the normal system execution.
  20. [Unity]多线程编程的一点心得

热门文章

  1. Metadata
  2. Luogu P1365 WJMZBMR打osu! / Easy
  3. RabbitMQ出现服务启动几秒退出问题
  4. TensorFlow学习笔记----例子(2)
  5. 洛谷P1464 Function
  6. 1.1、配置Python虚拟环境
  7. 55.TF/IDF算法
  8. Java设计模式之 — 策略(Strategy)
  9. 6.在idea中链接数据库
  10. 【Educational Codeforces Round 53 (Rated for Div. 2) C】Vasya and Robot