Search This Blog

Monday, September 21, 2009

Removing Duplicates from STL Vector


#include <vector>
#include <algorithm>
using namespace std;
// Add this header files


vector<CString> vNames;
vector<CString> ::iterator vItr;
CString csName;

vNames.push_back("John");
vNames.push_back("Victor");
vNames.push_back("Nancy");
vNames.push_back("William");
vNames.push_back("Nancy");
sort(vNames.begin(),vNames.end());
vNames.erase(unique(vNames.begin(),vNames.end()),vNames.end());

for(vItr=vNames.begin();vItr!=vNames.end();vItr++)
{
csName=(*vItr);
//TRACE(csName);
//AfxMessageBox(csName);
}

No comments: