1 #include<iostream> 2 #include<string> 3 #include<algorithm> 4 #include<unordered_set> 5 6 using namespace std; 7 8 /*給出一個字符串,將重復的字符去除,僅保留第一次出現的字符,且保持去重后的字符在原字符串中的順序不變。 9 10 輸入數據是一個字符串(不包含空格) 11 12 輸出去重后的字符串 13 14 輸入:12ere2 15 16 輸出:12er 17 */ 18 19 int main() 20 { 21 string instr,outstr; 22 unordered_set<char> sc; //用來去重字符串中重復字符 23 getline(cin,instr); 24 for(auto c:instr) 25 { 26 if(sc.find(c) == sc.end()) // 如果set中沒找到該字符 27 { 28 sc.insert(c); 29 outstr.append(1,c); 30 } 31 } 32 cout<< outstr <<endl; 33 return 0; 34 }