0

我有以下代码

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; 
    };

现在我有一个简单的排序代码,其中我根据另一个双精度向量对索引向量进行排序。'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;
}

现在在控制台中,我得到这样的结果:

 RHS Out of range: 1711 : 1079615151 vector::_M_range_check

由于我没有使用任何自定义类,并且我正在排序的向量仅基于双打,所以我不确定为什么我会超出范围。我确保双向量中没有无穷大,但即使有,std::sort 不应该在不超出索引的情况下给我正确的排序索引吗?

感谢您的任何帮助。

编辑:如果有帮助,这是发生这种情况时向量的数据转储。 http://pastebin.com/7wLX63FJ另外,我正在使用 Xcode 3.2.6 附带的 GCC 4.2 进行编译。

4

1 回答 1

2

此错误是由nan数据中的值(位置 1688)引起的。问题是<不再满足包含sstd::sort时所需的约束。nan有关所有比较器必须满足的“严格弱排序”的定义,请参见标准 25.4/4。

于 2013-02-09T04:29:42.503 回答