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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:14:47+00:00 2026-05-18T20:14:47+00:00

I know this may be a silly question. but stil i have a confusion.

  • 0

I know this may be a silly question. but stil i have a confusion.
W.r.t std::map. i have written a custom predicate for dymanic ordering of map,

enum OrderingType 
{
    ASCENDING, 
    DESCENDING 
};

template <class T>
class Ordering
{
    OrderingType m_order;

public:
    Ordering(OrderingType order) : m_order(order) { }

    bool operator() (const T &obj1, const T &obj2)
    {
        if( m_order == ASCENDING )
            return obj1 < obj2;

        if( m_order == DESCENDING )
            return obj1 > obj2;
    } 
};

Advantage is

  1. We can decide the ordering of data elements in the map on some conditions

    OrderType type = (condition ? ASCENDING : DESCENDING );
    CUSTOMMAP m(type);

  2. We can use same forward iterator for both ascending & descending ordered map

    in the below code. map’s ordering works fine in both ascending & descending order ( amp1 & map2 ). But on assignment map2 = map1, the ordering of map2 changes along with the content. I was expected to copy only content, not the change in the ordering. further inserts on map2 ( which was declared as descending) will be in ascending order.

Any suggestions or idea..? or defining two way ordering predicate for map is bad idea..?

typedef map<int, int, Ordering<int> >  CUSTOMMAP;
typedef CUSTOMMAP::iterator       CUSTOMMAP_ITER;
typedef CUSTOMMAP::const_iterator CUSTOMMAP_CONST_ITER;

ostream& operator <<(ostream& out, const CUSTOMMAP& mapobj)
{
    CUSTOMMAP_CONST_ITER citer = mapobj.begin();
    for( ; citer != mapobj.end(); ++citer )
    {
        out << citer->first << "   " << citer->second << endl;
    } 
    out << "==========" << endl;
    return out;
}

int main()
{
    CUSTOMMAP map1(ASCENDING);     //instantiate a map with ascending sorting
    CUSTOMMAP map2(DESCENDING);    //instantiate a map with descending sorting

    map1.insert( make_pair(1, 0));
    map1.insert( make_pair(2, 0));
    cout << map1;                  // prints data in ascnding manner 

    map2.insert( make_pair(5, 0));
    map2.insert( make_pair(6, 0));
    cout << map2;                  // prints data in descending manner 

    map2 = map1;

    cout << map2;                  //copys contents of map1 to map2 & changes 
                                   //map2's ordering predicate
    return 0;
}
  • 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-18T20:14:47+00:00Added an answer on May 18, 2026 at 8:14 pm

    Well setting map2 = map1 is going to in fact copy the whole object, not just the elements. What you probably want to do then is

    map2.clear();
    map2.insert(map1.begin(), map1.end());
    

    I believe off the top of my head that the complexity of the two steps together is going to be O(n log n), but don’t quote me on that.

    Edit

    Even better (O(n)):

    map2.clear();
    map2.insert(map1.rbegin(), map1.rend());
    

    “For the third version ( insert (first,last) ), Nlog(size+N) in general (where N is the distance between first and last, and size the size of the container before the insertion), but linear if the elements between first and last are already sorted according to the same ordering criterion used by the container.” (http://cplusplus.com/reference/stl/map/insert)

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

Sidebar

Related Questions

I know this may be a silly question. but stil i have a confusion.
I know this may be a silly question for experienced coders. But I have
Now this may seem like a silly question, but I need to know how
This may be a silly question but... Say you have a sentence like: The
I know this may be a silly question but for this application I cannot
This may be a silly question, but does anyone know of a proof that
Hi guys this question may look to be very silly. But still i have
This question may sound silly, but I have no idea how to solve this:
I know this question may sound silly, but I am learning (at least trying)
This may be a silly question, but I need to know it anyway. I

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.