您的位置:首页 > 其它

华为OJ(找出字符串中第一个只出现一次的字符)

2015-08-10 11:33 453 查看
题目本来很简单,想用map实现,但还是too young,动手太少,代码很简单:

#include<iostream>
#include<map>
#include<string>
using namespace std;
int main()
{
string str;
char temp;
bool flag=false;
map<char,int> word;
getline(cin,str);
for(int i=0;i<str.size();++i)
++word[str[i]];

for(int i=0;i<str.size();++i)
{
temp=str[i];
if(word[temp]==1)<span style="white-space:pre">	</span>//开始错写成if(word.count(temp)==1)
{
flag=true;
break;
}
}
if(flag==false)
temp='.';
cout<<temp<<endl;
//system("pause");
return 0;
}
我就纳闷怎么答案不对呢,原来时间太长忘了count的功能是对键的计数,对map而言,count返回值只能是0或1
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: