代码如下:
1 #include<iostream> 2 #include<map> 3 #include<cstdio> 4 #include<cstring> 5 using namespace std; 6 int main(){ 7 char str1[35] ; 8 char key[35] , value[35] ; 9 map<string , string> map_ ; 10 while ( gets(str1) ){ 11 if ( strcmp(str1 , "\0") == 0 ){ 12 break ; 13 } 14 sscanf(str1 , "%s %s" , value , key) ; ///从一个字符串中读进与指定格式相符的数据 15 // map_[key] = value ; ///两种建立键值的关系均可; 16 map_.insert(make_pair(key , value)) ; 17 } 18 while ( gets(str1) ){ 19 map<string , string>::iterator it ; ///迭代器 20 it = map_.find(str1) ; ///查找map_中是否有此键 21 if ( it != map_.end() ){ ///如果查找到 22 cout << it->second << endl ; ///输出键值 23 }else{ 24 cout << "eh" << endl ; ///否则输出 eh 25 } 26 } 27 return 0 ; 28 }
POJ 2503 Babelfish (c++)(map容器)
原文:https://www.cnblogs.com/Cantredo/p/9244921.html