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 8568139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:02:31+00:00 2026-06-11T18:02:31+00:00

I am trying to implement a simple comparator for sorting indices based on values

  • 0

I am trying to implement a simple comparator for sorting indices based on values in an array “_vec”. I am getting an “invalid < operator” run-time error message. I fail to understand what is wrong with the following code:

class Compare{
vector<int>& _vec;
public:
    Compare(vector<int>& vec) : _vec(vec) {}
    bool operator()(size_t i, size_t j){
        if(_vec[i] != _vec[j])
        return _vec[i] < _vec[j];
        else
        return (double)rand()/RAND_MAX < 0.5; 
    }
};

I am using the following function call:

sort(inds.begin(),inds.end(),Compare(vals));

where inds is just an array containing indices from 1 to 15 (say) and vals is the array of length 15 with some values whose sorted indices I want to compute. The overall goal is to randomize the sort order when two (or more) entries in vals are equal. Any help?

  • 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-11T18:02:33+00:00Added an answer on June 11, 2026 at 6:02 pm

    std::sort() expects the comparison operation to be stable – if a particular result is returned when two items are compared, the comparison must return the same result if those items are compared again later. When you return a random value, obviously that’s not necessarily going to hold.

    C++03 25.3/4 “Sort and related operations” says:

    If we define equiv(a, b) as !comp(a, b) && !comp(b, a), then the
    requirements are that comp and equiv both be transitive relations:

    • comp(a, b) && comp(b, c) implies comp(a, c)
    • equiv(a, b) && equiv(b, c) implies equiv(a, c)

    [Note: Under these conditions, it can be shown that

    • equiv is an equivalence relation
    • comp induces a well-defined relation on the equivalence classes determined by equiv
    • The induced relation is a strict total ordering.

    —end note]

    And for clarification, Table 28 defines an equivalence relation:

    == is an equivalence relation, that is, it satisfies the following
    properties:

    • For all a, a == a.
    • If a == b, then b == a.

    So your Compare() operation will not produce an equivalence relation.

    It’s kind of nice to get an assertion rather than losing data randomly.

    One way to solve the problem of randomizing the sort order when two (or more) entries in _vec are equal is to make up a random value for those indexes and keep track of those random values in a map of index => random value or something. Just make sure that you keep track of and use those random values in such a way that the transitive and equivalence properties of Compare() hold.

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

Sidebar

Related Questions

I'm getting acquanted with Core Animation and trying to implement simple movement along a
I am trying to implement simple xhr abstraction, and am getting this warning when
I'm trying to implement simple sliding window alogirithm on two-dimensional array in C# 3.0,
I'm trying to implement Simple chat using Servlet 3.0 and Comet pattern based on
I'm trying to implement some simple paging, based on How do I do pagination
Trying to implement a simple notification system based on private pub ( something like
I'm trying to implement a simple queue that performs one task at a time.
I'm trying to implement a simple queue that performs one task at a time.
i'm trying to implement a simple array of function descriptors of type fun_desc struct
I've been trying to implement simple low level keyhook using JNI and all went

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.