Erase and unique combined for repetitive elements deleted

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

STL unique can only repeat elements to the end of the container, and can not really repeat elements deleted. Here the use of unique and erase can be completely removed effect

The sample code is as follows:

#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<<"Please input";
	  cin>>temp;
	  pos=remove(test.begin(),test.end(),temp);//Only the temp value null and put in the last one did not completely remove
	  // Remove returns the last not removed element to the next position 
	  test.erase(pos,test.end());  //Deletion of POS to test.end () of all the elements
	  for_each(test.begin(),test.end(),print);
	  //If test is list<string> direct test.remove (Temp) can completely remove
}

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Jason at November 13, 2013 - 12:11 PM