You can solve this by passing a case-insensitive predicate to both sort()
and unique()
:
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
string lowercase(string x)
{
transform(x.begin(), x.end(), x.begin(), [](char x) { return tolower(x); });
return x;
}
int main()
{
vector<string> stringVec = {"John", "Bob", "Joe", "Zack", "Randy", "bob", "joe", "andy"};
sort(stringVec.begin(), stringVec.end(),
[](const string &a, const string &b) { return lowercase(a) < lowercase(b); }
);
stringVec.erase(
unique(stringVec.begin(), stringVec.end(),
[](const string &a, const string &b) { return lowercase(a) == lowercase(b); }
),
stringVec.end()
);
for(unsigned i=0; i<stringVec.size(); i++)
{
cout<<stringVec[i]<<endl;
}
return 0;
}