ios::sync

ios::sync,第1张

class="superseo">ios::sync_with_stdio(false); 的副作用

看这里
look here
come on

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 
//#include 
//#include 
//#include 
//#include 
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(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(b,a));
	mp.insert(make_pair(b,a));
} 
while(cin>>str){//~scanf("%s",str)
//	map::iterator it;
//	it=mp.find(str);
//	if(it!=mp.end()){
//			cout<second<
//	}
//	else cout<<"eh"<
		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::iterator it;
// it=mp.find(str);
// if(it!=mp.end()){
// cout // }
// else cout<<“eh”< if(mp.count(str)){
cout< }
else cout<<“eh”< }

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/993958.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存