I’m trying to sort
std::vector< std::vector< std::string> > perm;
I am attempting to use std::sort from the algorithm header
I called it with
std::sort( perm.begin(), perm.end(), sortPerms);
This is my sort function:
bool sortPerms (const std::vector<std::string> &i, const std::vector<std::string> &j) {
for(unsigned int x = 0; x < i.size(); x++) {
if(i[x] != j[x])
return false;
}
//both are equal
return true;
}
The purpose of sorting is to then call std::unique to obtain a vector with unique values.
When I compile with gcc in cygwin, I get no errors but I have repeats, and when I compile with visual studio 2010, I get an error that operator< is not defined.
I stepped through and it is attempting to use its own sort function, and not the one that I have defined.
I’m not sure how to fix this, any suggestions?
Other details:
It is guaranteed that all vectors will be of same size.
It’s purpose is a vector of every permutation of an original vector of strings.
Each string is a command, and I am looking for all of the different ways these commands can be shuffled. So I need to strip the duplicates.
Your sort function should return when
a < b, nota != b.Also, by default,
std::vector<>should already support lexicographic comparison viaoperator<, which should do what you’re expecting (compare each element in turn, with the first non-equivalent elements used for the comparison). See http://en.cppreference.com/w/cpp/container/vector/operator_cmpYour current sortPerms function looks like what you would pass to
std::unique, but that has different behavior than a predicate used forstd::sort.