STL算法 unique的用法

来源:互联网 发布:苏宁淘宝预售靠谱吗 编辑:程序博客网 时间:2024/05/21 17:09
string str;    vector<string> words;    while(cin>>str){        words.push_back(str);    }    sort(words.begin(),words.end());    vector<string>::iterator end_unique =                            unique(words.begin(),words.end());    words.erase(end_unique,words.end());


先排序,才能用unique,

unique将相邻的重复的元素移到最后,返回一个iterator指向最后的重复元素

再用erase删除就达到了去除重复的目的


注:算法不直接修改容器的大小。如果需要添加或删除元素,则必须使用容器操作。

原创粉丝点击