map使用及例题

map 的使用 注意first 为key值 second 是value值

然后就是在杭电上头文件对于map的map<string,int >::iterator i;的操作。使用#include会编译错误。。则不会

对于hdu1263,由于map<string,int>存储是按KEY值的字母顺序排序,所以这里免去了排序的步骤。

STL很强大!

hdu1004:

[cpp] view plain copy
#include
#include
#include
using namespace std;
map<string,int>M;
map<string,int>::iterator p,q;
int n;
int main()
{
string str;
while(scanf("%d",&n),n)
{
M.clear(); //清空
while(n–)
{
cin>>str;
if(M[str]==0) //插入
M[str]=1;
else
M[str]++;
}
int k=-1;
for(p=M.begin();p!=M.end();p++) //查找
{
if((p->second)>k)
{
k=p->second;
q=p;
}
}
cout<first<<endl;
}
return 0;
}
#include
#include
#include
using namespace std;
map<string,int>M;
map<string,int>::iterator p,q;
int n;
int main()
{
string str;
while(scanf("%d",&n),n)
{
M.clear(); //清空
while(n–)
{
cin>>str;
if(M[str]==0) //插入
M[str]=1;
else
M[str]++;
}
int k=-1;
for(p=M.begin();p!=M.end();p++) //查找
{
if((p->second)>k)
{
k=p->second;
q=p;
}
}
cout<first<<endl;
}
return 0;
}

然后是hdu1075

[cpp] view plain copy
#include <stdlib.h>
#include
#include <stdio.h>
#include
#include
using namespace std;
map<string,string> m;
map<string,string>::iterator it;
char c[3020];
int main()
{
char s[22],t[22],tmp[22];
scanf("%s",s);
while(scanf("%s",s)!=EOF)
{
if(!strcmp(s,“END”))
break;
scanf("%s",t);
m[t]=s;
}
scanf("%s",s);
getchar();
while(gets©)
{
if(!strcmp(c,“END”))
break;
int len=strlen©;
int j=0;
for(int i=0;i<len;i++)
{
if(c[i]>=‘a’&&c[i]<=‘z’)
{
tmp[j]=c[i];
j++;
}
else
{
tmp[j]=’\0’;
it=m.find(tmp);
if(it!=m.end())
{
char ww[22];
//ww=it->second;
cout<second;
//printf("%s",it->second);
}
else
{
printf("%s",tmp);
}
printf("%c",c[i]);
j=0;
}
}
printf("\n");
}
return 0;
}
#include <stdlib.h>
#include
#include <stdio.h>
#include
#include
using namespace std;
map<string,string> m;
map<string,string>::iterator it;
char c[3020];
int main()
{
char s[22],t[22],tmp[22];
scanf("%s",s);
while(scanf("%s",s)!=EOF)
{
if(!strcmp(s,“END”))
break;
scanf("%s",t);
m[t]=s;
}
scanf("%s",s);
getchar();
while(gets©)
{
if(!strcmp(c,“END”))
break;
int len=strlen©;
int j=0;
for(int i=0;i<len;i++)
{
if(c[i]>=‘a’&&c[i]<=‘z’)
{
tmp[j]=c[i];
j++;
}
else
{
tmp[j]=’\0’;
it=m.find(tmp);
if(it!=m.end())
{
char ww[22];
//ww=it->second;
cout<second;
//printf("%s",it->second);
}
else
{
printf("%s",tmp);
}
printf("%c",c[i]);
j=0;
}
}
printf("\n");
}
return 0;
}

最后hdu1263

[cpp] view plain copy
#include
#include
#include
#include
using namespace std;

int main()
{
map<string,map<string,int> > a;
map<string,int> b;
map<string,map<string,int> >::iterator i;
map<string,int>::iterator j;
int T,m,tmp;string s,tb;
cin>>T;
while(T–)
{
a.clear();
b.clear();
cin>>m;
while(m–)
{
cin>>s>>tb>>tmp;
a[tb][s]+=tmp;
}
for(i=a.begin();i!=a.end();i++)
{
cout<first<<endl;
b=i->second;
for(j=b.begin();j!=b.end();j++)
{
cout<<" |----"<first<<"("<second<<")"<<endl;
}
}
if(T!=0)
printf("\n");
}
}
#include
#include
#include
#include
using namespace std;

int main()
{
map<string,map<string,int> > a;
map<string,int> b;
map<string,map<string,int> >::iterator i;
map<string,int>::iterator j;
int T,m,tmp;string s,tb;
cin>>T;
while(T–)
{
a.clear();
b.clear();
cin>>m;
while(m–)
{
cin>>s>>tb>>tmp;
a[tb][s]+=tmp;
}
for(i=a.begin();i!=a.end();i++)
{
cout<first<<endl;
b=i->second;
for(j=b.begin();j!=b.end();j++)
{
cout<<" |----"<first<<"("<second<<")"<<endl;
}
}
if(T!=0)
printf("\n");
}
}

MapReduce的例题中,有一个叫做GroupReduce的类,它是一个Reducer类。在这个类中,我们可以看到reduce()方法的实现。这个方法接收一个OrderBean类型的key和一个Iterable<Text>类型的values作为参数,同时还有一个Context对象用来写出结果。在reduce()方法中,我们可以看到通过一个for循环来遍历values,然后将每一个value写出到结果中。同时,还有一个变量i用来记录写出的数据条数,并且在写出一条数据后,会进行判断,如果i大于等于1,则直接退出循环。这样,就实现了只取第一条数据的功能。这是一个处理数据的例子,它展示了MapReduce中如何使用Reducer来处理数据并输出结果。 除此之外,关于MapReduce的例题还有很多。例如,在一个例题中,我们可以使用MapReduce来统计一篇文章中每个单词出现的次数。在这个例子中,Map阶段负责将文章切分成单词,并将每个单词作为key,将其出现的次数作为value输出。Reduce阶段负责对相同单词的出现次数进行累加,并将结果输出。 另一个例题使用MapReduce来进行排序。在这个例子中,Map阶段负责读取输入数据,并按照某个字段进行排序,然后将排序结果作为key,并将原始数据作为value输出。Reduce阶段负责对相同key的数据进行归并,并将结果输出。通过这个例子,我们可以看到MapReduce的排序功能。 以上是一些关于MapReduce的例题,它们展示了MapReduce在数据处理和排序方面的应用。通过这些例子,我们可以更好地理解和掌握MapReduce的使用
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值