题目链接:http://poj.org/problem?id=2503
题目大意:就是给你一本词典,问你能否在词典中找到你要查询单词的意思,不能就输出eh
思路:map的入门级题,直接词典中的词组存到map中,然后直接查询。就是有些细节需要注意
code:
#include<cstdio> #include<iostream> #include<cmath> #include<string> #include<map> using namespace std; int main() { map<string,string> m1; map<string,string>::iterator m1_it; string s1,a,b; while(getline(cin,s1)) { int pos=s1.find(" "); a.assign(s1,0,pos); b.assign(s1,pos+1,s1.size()); //cout<<a<<" "<<b<<endl; m1[b]=a; while(getline(cin,s1)) { if(s1=="\0") break; //用“\0”判断结束 int pos=s1.find(" "); a.assign(s1,0,pos); b.assign(s1,pos+1,s1.size()); //cout<<a<<" "<<b<<endl; m1[b]=a; } while(getline(cin,s1)) { if(s1=="\0") break; m1_it=m1.find(s1); //查找 if(m1_it==m1.end()) { cout<<"eh"<<endl; } else { cout<<m1_it->second<<endl; } } } return 0; }
原文地址:http://blog.csdn.net/u010304217/article/details/39482341