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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:48:34+00:00 2026-05-17T18:48:34+00:00

I have a vector of doubles. I want to sort it from highest to

  • 0

I have a vector of doubles. I want to sort it from highest to lowest, and get the indices of the top K elements. std::sort just sorts in place, and does not return the indices I believe. What would be a quick way to get the top K indices of largest elements?

  • 1 1 Answer
  • 1 View
  • 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-05-17T18:48:34+00:00Added an answer on May 17, 2026 at 6:48 pm

    The first thing that comes to mind is somewhat hackish, but you could define a struct that stored both the double and its original index, then overload the < operator to sort based on the double:

    struct s {
        double d;
        int index;
        bool operator < (const struct &s) const {
            return d < s.d;
        }
    };
    

    Then you could retrieve the original indices from the struct.

    Fuller example:

    vector<double> orig;
    vector<s> v;
    ...
    for (int i=0; i < orig.size(); ++i) {
        s s_temp;
        s_temp.d = orig[i];
        s_temp.index = i;
        v.push_back(s);
    }
    sort(v.begin(), v.end());
    //now just retrieve v[i].index
    

    This will leave them sorted from smallest to largest, but you could overload the > operator instead and then pass in greater to the sort function if wanted.

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

Sidebar

Related Questions

I currently have a std::vector which holds std::vector of double. I'd want to sort
i have a std::vector, namely vector<vector<vector> > > mdata; i want pass data from
I have the vector d<-1:100 I want to sample k=3 times from this vector
I have trouble with pointers and references to doubles. I want to access elements
I have the following template declaration: template <typename T> void IterTable(int& rIdx, std::vector<double>& rVarVector,
I have vector< pair<int, int>> myVec (N); I want to have all pairs initialized
I have a matrix A with size (nr,nc), a vector of column indices B
First I want to say that, I have a vector which has thousand of
I want to have 2d vector classes for every primitive type. Right now, to
I have a 1D vector(Vp_cpp) and I want to convert to 2D vector(declared as

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.