Reputation: 621
I have the following code
template<typename T>
bool GenericCompare(T lhs, T rhs)
{
return lhs < rhs;
}
template<typename T>
class SortOrder
{
public:
SortOrder(const std::vector<T> *_sortArray,
bool (*_comparator)(T,T) = GenericCompare) :
sortArray(_sortArray) , comparator (_comparator) , customOperator(true) {;}
bool operator()(int lhs=0, int rhs=0) const
{
bool res;
try {
sortArray->at(lhs);
}
catch (std::out_of_range& oor) {
std::cout << "LHS Out of range: " << lhs << " : " << rhs
<< " " << oor.what() << std::endl;
}
try {
sortArray->at(rhs);
}
catch (std::out_of_range& oor) {
std::cout << "RHS Out of range: " << lhs << " : "
<< rhs << " "<< oor.what() << std::endl;
}
// Always needs comparator
res = comparator(sortArray->at(lhs),sortArray->at(rhs));
return res;
}
private:
const std::vector<T> *sortArray;
bool (*comparator)(T,T);
bool customOperator;
};
Now I have a simple sorting code in which I sort an index vector based on another vector which is a double. 'circle_fwd_vector' is a vector containing all doubles.
for (int i=0;i<circle_fwd_vector.size();i++) {
circle_index_vector.push_back(i);
}
try {
std::sort(circle_index_vector.begin(),circle_index_vector.end(),
SortOrder<double>(&circle_fwd_vector));
}
catch (std::exception& e)
{
std::cout << e.what() << std::endl;
}
Now in the console, I'm getting a result like this:
RHS Out of range: 1711 : 1079615151 vector::_M_range_check
Since I'm not using any custom class and the vector I'm sorting is based on just doubles I'm not sure why I am getting this out of range. I made sure there are no infinities in the double vector, but even if there are, shouldn't std::sort still give me the correct sorted index without going out of index?
Thank you for any help.
Edit: If it helps, here is the data dump of the vector when this happens. http://pastebin.com/7wLX63FJ Also, I'm compiling this using GCC 4.2 that ships with Xcode 3.2.6.
Upvotes: 0
Views: 544
Reputation: 16253
This error is caused by the nan
value in your data (position 1688). The problem is that <
is no longer satisfies the constraints required by std::sort
when you include nan
s. See the standard, 25.4/4, for the definition of the "strict weak ordering" that all comparators have to satisfy.
Upvotes: 2