使用C++ map实现,比较简单。 // 程序员面试题精选100题(13)-第一个只出现一次的字符.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include<iostream> #include<string> #include<map> using namespace std; char FirstNotRepeatingChar(char *pString); int _tmain(int argc, _TCHAR* argv[]) { string s; cin>>s; const char *str=s.c_str(); char p[100]; strcpy(p,str); cout<<FirstNotRepeatingChar(p)<<endl; system("pause"); return 0; } char FirstNotRepeatingChar(char *pString) { map<char,int> count; char *p=pString; while(*p!='/0') ++count[*p++]; map<char,int>::const_iterator it=count.begin(); while(it!=count.end()){ if(it->second==1){ return it->first; break; } it++; } }