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

来源:互联网 发布:网络摄像头改ip软件 编辑:程序博客网 时间:2024/06/06 08:46
#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,重复