Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9258123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:18:58+00:00 2026-06-18T12:18:58+00:00

I have the following code template<typename T> bool GenericCompare(T lhs, T rhs) { return

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T12:18:59+00:00Added an answer on June 18, 2026 at 12:18 pm

    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 nans. See the standard, 25.4/4, for the definition of the “strict weak ordering” that all comparators have to satisfy.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: #include <iostream> using namespace std; template<typename T> class myclass
I have the following code: template<class T> class RandomTreeNode { public: typedef typename RandomTreeFunction<T>::function_ptr
I have the following code: template<typename T, typename Allocator = std::allocator<T> > class Carray
I have the following code that compiles and works well: template<typename T> T GetGlobal(const
I have the following code: class TimeOutException {}; template <typename T> class MultiThreadedBuffer {
I have the following code: template <class T> struct pointer { operator pointer<const T>()
Please have a look at the following code template <typename T> class Stack {
Please have a look at the following code, Stack.h template <typename T> class Stack
Consider the following code : template<bool AddMembers> class MyClass { public: void myFunction(); template<class
gcc 4.5.1, SuSE Linux i686 Suppose we have following code: template<typename realT> class B

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.