Ok, the problem is that you are misunderstanding how to write comparison functions.
Here's a working example of std::sort. Look carefully to see how my implementation differs from yours. You are misunderstanding what parameters are getting passed to operator().
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
struct Person {
string firstName;
string lastName;
};
struct AscendingSortByLastName {
// Here is the function that will be called by std::sort:
bool operator()( const Person& lhs, const Person& rhs ) {
if( lhs.lastName < rhs.lastName ) return true;
if( rhs.lastName < lhs.lastName ) return false;
return lhs.firstName < rhs.firstName;
}
};
// Assume we have a vector of Person and it has some elements in it.
vector< Person > people;
// To sort:
std::sort( people.begin(), people.end(), AscendingSortByLastName() );
|
Note that the comparator does not have state; it does not contain the vector
to sort. The comparison function is passed two elements from the container
you are sorting (in my case Person) and needs to return whether or not
the first is less than the second.
In your code, your comparison function on std::sort() is taking two UINT32
(because your vector is a vector of UINT32) and is treating these values
as
indices into the vector. They are not indices; they are actual values
of two elements in the vector. Since operator< is already defined for UINT32,
you don't even need a comparison function for that std::sort() call.
You have similar problems on your find_if and lower_bound calls.