您的位置:首页 > 其它

【整理】删除顺序容器(如:vector)中的重复字符串

2013-01-16 17:25 190 查看
#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,重复
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: