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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:21:48+00:00 2026-05-27T12:21:48+00:00

Consider a std::map<K,V> . I want to re-order the map by value profiting by

  • 0

Consider a std::map<K,V>. I want to re-order the map by value profiting by an appropriate container std::C<V*> or std::C<V&>, in a way that no copies of values are done to store the elements in C. Furthermore, elements in C must be sorted according to the result of int f(V&) applied to each element. Despite my efforts I could not find an appropriate C and an enough efficient way to build it. Do you have any solution? A small example would be much appreciated.

  • 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-27T12:21:49+00:00Added an answer on May 27, 2026 at 12:21 pm

    Seems simple enough.

    std::map<K,V> src;
    int f(V&) {return 0;}
    
    V* get_second(std::pair<const K,V> &r) {return &(r.second);} //transformation
    bool pred(V* l, V* r) { return f(*l)<f(*r); }   //sorting predicate
    
    std::vector<V*> dest(src.size());  //make destination big enough
    std::transform(src.begin(), src.end(), dest.begin(), get_second); //transformcopy
    std::sort(dest.begin(), dest.end(), pred); //sort
    

    Unless you meant C is supposed to be another map:

    std::pair<K,V*> shallow_pair(std::pair<const K,V> &r) 
    {return std::pair<K,V*>(r.first, &(r.second));}
    
    std::map<K, V*> dest2;
    std::transform(src.begin(), src.end(), 
                   std::inserter(dest2,dest2.end()), shallow_pair);
    

    http://ideone.com/bBoXq

    This requires the previous map to remain in scope longer than dest, and have no pairs removed until dest is destructed. Otherwise src will need to have been holding smart pointers of some sort.

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

Sidebar

Related Questions

Consider this situation: void doSmth1(std::map<int,int> const& m); void doSmth2(std::map<int,int> const& m) { std::map<int,int> m2
Consider the following code: int&& x = 42; static_assert(std::is_same<decltype( x ), int&&>::value, &&); static_assert(std::is_same<decltype((x)),
Consider the following code. I want to specialize std::hash<> used in a Map ,
Consider this program: #include <map> #include <vector> typedef std::vector<int> IntVector; typedef std::map<IntVector,double> Map; void
Consider the following snippet: #include <map> class A { static std::map<int,int> theMap; #pragma omp
For a specific example, consider atoi(const std::string &) . This is very frustrating, since
Consider the following code: typedef std::vector<int> cont_t; // Any container with RA-iterators typedef cont_t::const_iterator
Consider: #include <map> int main() { std::map< int, int > m; m[ 0 ]
consider this translation unit: #include <map> #include <string> int main() { std::map<std::string, std::size_t> mp;
Consider the following snippet: struct ObjectInterface { virtual ~ObjectInterface() {} virtual void Print(std::ostream& target)

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.