ios::sync_with_stdio(false); 的副作用
sync_with_stdio() 用处是“关闭同步”,从而加快cin与cout的效率。
在部分机子上如果开了这个函数cin和cout跑的还比printf和scanf快。
但是用了sync_with_stdio(false)之后不能与printf和scanf同用,否则会出错。
所以,
要不就全部scanf,printf,
要不就ios::sync_with_stdio(false)+全部cin,cout,
不要混用避免不必要的WA.
#include <bits/stdc++.h>
//#include <iostream>
//#include <stdio.h>
//#include <algolrithm>
//#include <map>
using namespace std;
map<string ,string> mp;
int main(int argc, char** argv) {
ios::sync_with_stdio(false); cin.tie(0);
// string str,a,b;
// while(getline(cin,str)){
// sscanf(str,"%s %s",a,b);
// mp[b]=a;//关键字,对应的值
mp.insert(pair<string,string>(b,a));
mp.insert(make_pair(b,a));
// }
//sscanf中的参数是const char*,string类型的a,b无法作为sscanf函数的参数
char a[15],b[15],str[30];
map<string,string> mp;
while(cin.getline(str,30)&&strlen(str)!=0){
sscanf(str,"%s %s",a,b);
// mp[b]=a;
// mp.insert(pair<string,string>(b,a));
mp.insert(make_pair(b,a));
}
while(cin>>str){//~scanf("%s",str)
// map<string,string>::iterator it;
// it=mp.find(str);
// if(it!=mp.end()){
// cout<<it->second<<endl;
// }
// else cout<<"eh"<<endl;
if(mp.count(str)){
cout<<mp[str]<<endl;
}
else cout<<"eh"<<endl;
}
return 0;
}
while(cin>>str){ //~scanf("%s",str)
这里scanf和cout混用就会wa,除非注释掉ios::sync_with_stdio(false)
// map<string,string>::iterator it;
// it=mp.find(str);
// if(it!=mp.end()){
// cout<second<<endl;
// }
// else cout<<“eh”<<endl;
if(mp.count(str)){
cout<<mp[str]<<endl;
}
else cout<<“eh”<<endl;
}