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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:38:46+00:00 2026-05-26T08:38:46+00:00

I have a set of data which in some occasion I need to sort

  • 0

I have a set of data which in some occasion I need to sort them in one way and some occasion in another way. For example, suppose the data set is a set of strings,{“abc”, “dfg”,…}. Sometimes I need to sort them in alphabetic order and sometimes by comparing their length.

Initially I used std::set as a container of my data and implemented 2 comparators, hoping that I can change the comparator of the set on the fly, cause the data is huge and it’s not a good idea to copy it from one set to another..I just want to sort it using different comparators from time to time. Is this possible or what’s the right way to do it?

  • 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-05-26T08:38:47+00:00Added an answer on May 26, 2026 at 8:38 am

    You have to specify the comparator of std::set at construction time.

    As a solution, I would maintain two ‘index’ sets instead, each referring to the actual collection. This would yield the greatest flexibility. To keep everything together, I suggest you wrap it up in a single class:

    // to be compiled, debugged etc..., but ideal
    // to grab  the idea
    // caveats: maintain the index objects whenever the collection
    // gets resized/reallocated etc...
    // so not to be written yourself, use an existing library :)
    template< typename T, typename comp1, typename comp2 >
    struct MultiIndex {
        std::deque<T> collection;
        std::set<T*, comp1> index1;
        std::set<T*, comp2> index2;
    
        void insert( const T& t ){
           collection.push_back(t);
           index1.insert( &collection.back() );
           index2.insert( &collection.back() );
        }
    };
    

    Boost library has such a class: Multiindex.

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

Sidebar

Related Questions

I have several objects set up in Core Data, one of which is Deck
I encountered a problem which I need some help with - suppose I have
I have a large set of data which I access via a generator/iterator. While
I'm pretty new to Entity Frameworks. I have a set of Static Data which
I have a data set which consists of an ID and a matrix (n
I have a set of data points in 3D space which apparently all fall
I have the swiss data set provided by R, which has the following form:
I have a set of controls bound to data, on which I would like
I have a mysql table field set as time type which stores data in
I have set the data to div element and append it to another div

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.