erase与unique结合用于彻底删除重复元素

STL中的unique只能把重复的元素全部放到容器末端,并不能真正把重复元素删除. 这里使用unique 和 erase 则可达到彻底删除效果

示例代码如下:

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;

void print(string temp)
{
	cout<<temp<<endl;
}

int main()
{
	vector<string> test;
	vector<string>::iterator pos;
	string temp;

	  for (int i=0; i<6;++i)
	  {
		  cin>>temp;
		  test.push_back(temp);
	  }

	  cout<<"请输入";
	  cin>>temp;
	  pos=remove(test.begin(),test.end(),temp);//仅仅把temp值赋空并且放到最后一位 没有彻底删除
	  // remove返回最后一个未被移除元素的下一位置 
	  test.erase(pos,test.end());  //删除pos到test.end()的所有元素
	  for_each(test.begin(),test.end(),print);
	  //若然test是list<string> 则直接 test.remove(temp)即可完成彻底删除
}
原文地址:https://www.cnblogs.com/lewiskyo/p/STL_Erase_unique.html