map是一类关联式容器。它的特点是增加和删除节点对迭代器的影响很小,除了那个操作节点,对其他的节点都没有什么影响。对于迭代器来说,可以修改实值,而不能修改key。
在zoj_2832中,map主要用来建立文件名(即字符串)和一个整数的关联,其中字符串为key,整数为value.
值得注意的一点是,代码34行注释中使用的map插入方法,出现BUG。当使用p[s]时,如果p中存在关键字为s的数据,返回对应的value,如果不存在p会自动建立一个关键字为s,value为0的数据。
zoj_2832
在zoj_3023中,无耻的利用了map内部红黑树自排序的性质,还要注意find的使用
#include<iostream>
#include<stdio.h>
#include<string>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;
int n,m,k,l,num;
bool flag=false;
vector< vector<int> >father;
vector<string>name;
map<string,int>p;
string res[500];
int main()
{
while(cin>>n)
{
if(flag)
cout<<endl;
else
flag=true;
num=0;
father.clear();
name.clear();
p.clear();
while(n--)
{
string s;
cin>>s;
k=p[s];
int fa=k;
if(!k)
{
//p.insert(map<string,int>::value_type(s,++num));
p[s]=++num;
vector<int>aa;
father.push_back(aa);
name.push_back(s);
fa=num;
}
cin>>m;
while(m--)
{
string c;
//getchar();
cin>>c;
l=p[c];
if(!l)
{
//p.insert(map<string,int>::value_type(c,++num));
p[c]=++num;
vector<int>aa;
father.push_back(aa);
name.push_back(c);
father[num-1].push_back(fa-1);
}
else
father[l-1].push_back(fa-1);
}
}
int size=father.size();
int num=0;
for(int i=0;i<size;i++)
{
if(!father[i].size())
{
res[num]=name[i];
++num;
}
}
sort(res,res+num);
for(int i=0;i<num;i++)
{
cout<<res[i]<<endl;
}
}
}
zoj_1899
#include<iostream>
#include<cstdio>
#include<map>
#include<string>
#define DEBUG 1
using namespace std;
map<string,int>p;
int main()
{
string s;
int total=0;
#if DEBUG
freopen("e:\\zoj\\zoj_1899.txt","r",stdin);
#endif
while(getline(cin,s))
{
if(s.size()!=0)
{
++total;
if(!p[s])
++p[s];
else
++p[s];
}
else
{
for(map<string,int>::iterator it=p.begin();it!=p.end();++it)
{
double b=100.0*(*it).second/total;
cout<<(*it).first<<" ";
printf("%.4lf\n",b);
}
cout<<endl;
total=0;
p.clear();
}
}
for(map<string,int>::iterator it=p.begin();it!=p.end();++it)
{
double b=100.0*(*it).second/total;
cout<<(*it).first<<" ";
printf("%.4lf\n",b);
}
total=0;
p.clear();
}
zoj_3023
#include<iostream>
#include<cstdio>
#include<map>
using namespace std;
int m,n,s;
map<int,int>p1;
map<int,int>p2;
int main()
{
freopen("e:\\zoj\\zoj_3023.txt","r",stdin);
while(cin>>m>>n&&m!=0&&n!=0)
{
p1.clear();
p2.clear();
int sum1=0;
for(int i=0;i<m;i++)
{
cin>>s;
p1[s];
sum1+=s;
}
int sum2=0;
for(int i=0;i<n;i++)
{
cin>>s;
p2[s];
sum2+=s;
}
int dif=sum1-sum2;
bool flag=false;
if(dif%2!=0)
cout<<-1<<endl;
else
{
for(map<int,int>::iterator it=p1.begin();it!=p1.end();++it)
{
int temp=it->first-dif/2;
map<int,int>::iterator it2;
it2=p2.find(temp);
if(it2!=p2.end())
{
cout<<it->first<<" "<<it2->first<<endl;
flag=true;
break;
}
}
if(flag==false)
cout<<-1<<endl;
}
}
}
分享到:
相关推荐
python库。 资源全名:numpy_stl-2.4.1-cp27-cp27m-macosx_10_11_x86_64.whl
numpy_stl-2.16.0-cp38-cp38-win32
STL源码剖析--侯捷_繁体转简体中文文字版STL源码剖析--侯捷_繁体转简体中文文字版STL源码剖析--侯捷_繁体转简体中文文字版STL源码剖析--侯捷_繁体转简体中文文字版STL源码剖析--侯捷_繁体转简体中文文字版STL源码...
STL源码剖析--教程精华STL源码剖析--教程精华STL源码剖析--教程精华STL源码剖析--教程精华STL源码剖析--教程精华STL源码剖析--教程精华STL源码剖析--教程精华
numpy_stl-2.16.0-cp37-cp37m-win_amd64
numpy_stl-2.10.1-cp27-cp27m-win_amd64
STL中map用法详解 非常详细,有说明和可以编译的例子
numpy_stl-2.13.0-cp36-cp36m-win_amd64
STL源码剖析--侯捷,STL源码剖析--侯捷STL源码剖析--侯捷STL源码剖析--侯捷STL源码剖析--侯捷
STL源码,所有STL源码程序都在眼下,希望你能学会
用STL写的WEB服务器,服务端与客户端,测试例子
numpy_stl-2.10.1-cp35-cp35m-win_amd64
STL源码剖析--侯捷_繁体和简体版集合
STL源码剖析--侯捷_繁体转简体中文文字版 学习STL原理必须看的书
numpy_stl-2.16.0-cp39-cp39-win_amd64
numpy_stl-2.16.0-cp38-cp38-win_amd64
STL、线段树代码库 关于队列和栈的有关STL的操作vc++实现
[COM本质论].STL源码剖析--侯捷.pdf [COM本质论].STL源码剖析--侯捷.pdf [COM本质论].STL源码剖析--侯捷.pdf [COM本质论].STL源码剖析--侯捷.pdf