目录
(2)sizeof()、strlen()、length()、size()
题:实现一个函数用来找出字符流中第一个只出现一次的字符。例如,当从字符流中只读出前两个字符"go"时,第一个只出现一次的字符是"g"。当从该字符流中读出前六个字符“google"时,第一个只出现一次的字符是"l"。如果当前字符流没有存在出现一次的字符,返回#字符。(c++)
解法一.hashmap
#include<iostream>
using namespace std;
#include<map>
class Solution
{
public:
//Insert one char from stringstream
void Insert(char ch)
{
str += ch;//将字符流保存在string中
charMap[ch]++;//统计字符流中每个字符出现的次数
cout<<"str:"<<str<<" key:"<<ch<<" value:"<<charMap[ch]<<" map:"<<endl;
map<char,int>::iterator iter;
for(iter = charMap.begin(); iter != charMap.end(); iter++)
cout<<iter->first<<' '<<iter->second<<endl;
}
//return the first appearen