博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 1004 MAP【STL__map_的应用】
阅读量:4604 次
发布时间:2019-06-09

本文共 1612 字,大约阅读时间需要 5 分钟。

强大的MAP,今天终于开始好好学习一次。

 

map内部是用红黑树维持的有序结构。

定义:map<int,string>mapStudent;

查找的时间复杂度为对数级别.

1.构造方法学习两种:

第一种:用insert函数插入pair数据,mapStudent.insert(pair<int, string>(0,"jiangjing"));

第二种:用数组方式插入数据

mapStudent[1] = "jiangjing1";  mapStudent[2] =  "jiangjing2";

2.遍历也学习两种:

第一种:用迭代器遍历map<int, string>::iterator iter;

for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)cout<
first<<" " <
second<

第二种:用数组遍历

for(int nIndex = 0; nIndex < nSize; nIndex++)cout<
<

3.用count函数来判定关键字是否出现,出现返回1,没出现返回0;

用find函数来定位数据出现位置,它返回的一个迭代器,当数据出现时,它返回数据所在位置的迭代器,如果map中没有要查找的数据,它返回的迭代器等于end函数返回的迭代器,程序说明:

iter = mapStudent.find(1);if(iter != mapStudent.end())cout<<"Find, the value is "<
second<
<<"Do not Find"<

 

source code:

//#pragma comment(linker, "/STACK:16777216") //for c++ Compiler#include 
#include
#include
#include
#include
#include
#include
#include
#include
#define ll long long#define Max(a,b) (((a) > (b)) ? (a) : (b))#define Min(a,b) (((a) < (b)) ? (a) : (b))#define Abs(x) (((x) > 0) ? (x) : (-(x)))using namespace std;const int INF = 0x3f3f3f3f;map
m;string a, aa;int main(){ int i, j, t, n, numCase = 0; while(cin >> n){ if(0 == n) break; m.clear(); for(i = 1; i <= n; ++i){ cin >> a; ++m[a]; } map
::iterator it; int Max = 0; for(it = m.begin(); it != m.end(); ++it){ if(it->second > Max){ Max = it->second; aa = it->first; } } cout << aa << endl; } return 0;}

 

转载于:https://www.cnblogs.com/wushuaiyi/p/4089871.html

你可能感兴趣的文章
队列实现霍夫曼树
查看>>
【Java】图片高质量缩放类
查看>>
Python :类中设置默认属性并修改
查看>>
磁盘管理综合测试
查看>>
Unity3d Shader开发(三)Pass(Pass Tags,Name,BindChannels )
查看>>
UMLet
查看>>
从父控件移除控件
查看>>
calc()制作自适应布局
查看>>
Markdown-写作必备
查看>>
关于在Java中 a!=a 值为真的解释(摘抄)
查看>>
C#串口小助手
查看>>
详解定位与定位应用
查看>>
【前端开发】 5分钟创建 Mock Server
查看>>
java 从键盘录入的三种方法
查看>>
使用jQuery和YQL,以Ajax方式加载外部内容
查看>>
pyspider 示例
查看>>
电路板工艺中的NPTH和PTH
查看>>
JNI实现JAVA和C++互相调用
查看>>
JAVA 笔记(一)
查看>>
js 循环读取 json的值
查看>>