【发布时间】:2017-04-30 08:37:53
【问题描述】:
任何人都可以完成以下程序吗?
谢谢。
void RemoveDuplicates (string& input)
{
string nonRepeatedChars (input);
sort(nonRepeatedChars.begin(), nonRepeatedChars.end());
//cout << nonRepeatedChars <<endl;
string::iterator it = unique(nonRepeatedChars.begin(), nonRepeatedChars.end());
//cout << nonRepeatedChars <<endl;
nonRepeatedChars.erase(it, nonRepeatedChars.end());
cout << "nonRepeatedChars = "<< nonRepeatedChars <<endl;
for(string::iterator i = input.begin(); i != input.end(); i++)
{
cout << "*i = " << *i <<endl;
size_t found = nonRepeatedChars.find(*i);
cout << "found = "<< found <<endl;
if (found != string::npos)
{
input.erase(i);
cout << "input = " << input <<endl;
}
else
{
nonRepeatedChars.erase(found, 1);
cout << "nonRepeatedChars = "<< nonRepeatedChars <<endl;
}
}
cout << "Final Input = " << input <<endl;
}
【问题讨论】:
-
欢迎来到 StackOverflow。请拨打tour,学习提出好问题stackoverflow.com/help/how-to-ask。如果您正在寻求有关调试代码的帮助,请参阅 ericlippert.com/2014/03/05/how-to-debug-small-programs 这个问题需要更多详细信息,以避免出现“请解决我的问题”的印象。或“请做我的功课。”您可能还想了解格式化以提高可读性。
-
输入是“bbccdddaaba”。预期的输出是“bcda”