I have this following code where I want to sort vector of string according to the last character of the string. I've done the following but the sorting is done by default rules.
Here's the overloading < part:
bool operator<(const string &s1, const string &s2){
return s1.at(s1.size() - 1) < s2.at(s2.size() - 1);
}
This is in main:
vector <string> nameList;
int n;
cin>>n;
while(n--){
string name;
char str[100];
cin>>str;
name += str;
nameList.push_back(name);
}
sort(nameList.begin(), nameList.end());
for(int i = 0; i < nameList.size(); i++)
cout<<nameList.at(i)<<endl;
Code in ideone: LINK
It's not being used, you have to pass it into
std::sort
- something like:As noted, your
operator<
is not being called,std::string
already has overloaded operators in thestd
namespace.There are two versions of
std::sort
, one that will useoperator<
and another that takes a custom predicate to sort the container. Add a custom predicate, you can still usedsort
to sort yourvector
as required;I've called it
lastchar
, but you can name it what ever is best. As an added bonus, if you can used C++11 or above, you can make the predicate a lambda.