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

来源:互联网 发布:程序员谈恋爱被动吗 编辑:程序博客网 时间:2024/04/28 14:42

题目本来很简单,想用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

0 0
原创粉丝点击