C++面试题——字符串中第一个不重复字符

字符串中第一个不重复字符
map版本

#include
#include
#include
using namespace std;

int main() {
     
	string str;
	map<char, int> m;
	cin >> str;
	for (int i = 0; i < str.length(); i++) {
     
		if (!m.count(str[i])) {
     
			m[str[i]] = 1;
		}
		else
		{
     
			m[str[i]]++;
		}
	}
	map<char, int>::iterator it;
	for (it = m.begin(); it != m.end(); it++) {
     
		cout << it->first << ":" << it->second << endl;
	}
	for (int i = 0; i < str.length(); i++) {
     
		//cout << str[i] << ":" << m[str[i]] << endl;
		if (m[str[i]] == 1) {
     
			cout << str[i] << endl;
			break;
		}
		
	}
	return 0;
}

你可能感兴趣的:(工作)