简体   繁体   English

std :: out_of_range在std :: sort期间使用自定义比较器

[英]std::out_of_range during std::sort with custom comparator

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. 现在我有一个简单的排序代码,我在其中根据另一个为double的向量对索引向量进行排序。 'circle_fwd_vector' is a vector containing all doubles. 'circle_fwd_vector'是包含所有双打的向量。

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? 我确定双向量中没有无穷大,但即使有,也不应该std :: sort仍然给我正确的排序索引而不退出索引?

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. http://pastebin.com/7wLX63FJ另外,我正在使用Xcode 3.2.6附带的GCC 4.2进行编译。

This error is caused by the nan value in your data (position 1688). 此错误是由数据中的nan值引起的(位置1688)。 The problem is that < is no longer satisfies the constraints required by std::sort when you include nan s. 问题是当你包含nan时, <不再满足std::sort所需的约束。 See the standard, 25.4/4, for the definition of the "strict weak ordering" that all comparators have to satisfy. 有关所有比较器必须满足的“严格弱排序”的定义,请参见标准25.4 / 4。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM