#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
using namespace std;
int main(int argc, const char * argv[])
{
stringstream sentence("the quick red fox jumps over the slow red turtle");
string word;
vector<string> words;
while (sentence >> word)
{
words.push_back(word);
}
sort(words.begin(), words.end());
vector<string>::iterator unque_iter = unique(words.begin(), words.end());
words.erase(unque_iter, words.end());
return 0;
}
说明: unique()函数返回的是指向没有重复内容的下一个位置。而且其并不是删除其中的重复元素,只是将其移到容器的末尾,所以还需要自己待用erase()来彻底删除.
C++,unique(),erase,重复
本文介绍了一个使用 C++ 实现字符串数组去重的例子。通过 `unique()` 和 `erase()` 函数组合使用,有效地移除数组中的重复元素,并保持原有的顺序不变。
1250

被折叠的 条评论
为什么被折叠?



