// 面试题50(二):字符流中第一个只出现一次的字符 // 题目:请实现一个函数用来找出字符流中第一个只出现一次的字符。例如,当从 // 字符流中只读出前两个字符"go"时,第一个只出现一次的字符是'g'。当从该字 // 符流中读出前六个字符"google"时,第一个只出现一次的字符是'l'。 #include <iostream> #include <limits> using namespace std; class CharStatistics { public: CharStatistics() : index(0)//后初始化index=0 { for (int i = 0; i < 256; ++i) occurrence[i] = -1; } void Insert(char ch) { if (occurrence[ch] == -1)//之前没有过,插入index occurrence[ch] = index; else if (occurrence[ch] >= 0)//之前出现过,设为-2 occurrence[ch] = -2; index++;//用来指示出现过一次的字符的顺序 } char FirstAppearingOnce() { char ch = '