Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. STL : Map.Insert always sort the inserted key - How to prevent this

STL : Map.Insert always sort the inserted key - How to prevent this

Scheduled Pinned Locked Moved C / C++ / MFC
c++helptutorialquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pandit84
    wrote on last edited by
    #1

    Hi , I am having a std map object. I have added key and value in the following order.

    mymap.insert (pair<int,int>(10,20)) ; //Key,value
    mymap.insert (pair<int,int>(5,20)) ;
    mymap.insert (pair<int,int>(104,20)) ;
    mymap.insert (pair<int,int>(1,20)) ;

    But while extracting the data from map I am getting this data in a sorted order (sorted by Key order). How can I prevent this . I am passing the data from this map to a device which needs the data as is from my map. Please help

    C S 2 Replies Last reply
    0
    • P pandit84

      Hi , I am having a std map object. I have added key and value in the following order.

      mymap.insert (pair<int,int>(10,20)) ; //Key,value
      mymap.insert (pair<int,int>(5,20)) ;
      mymap.insert (pair<int,int>(104,20)) ;
      mymap.insert (pair<int,int>(1,20)) ;

      But while extracting the data from map I am getting this data in a sorted order (sorted by Key order). How can I prevent this . I am passing the data from this map to a device which needs the data as is from my map. Please help

      C Offline
      C Offline
      Chris Losinger
      wrote on last edited by
      #2

      maps are always sorted. the tree structure that they use internally requires it. if you want an unsorted container, use a vector (or a list).

      image processing toolkits | batch image processing

      1 Reply Last reply
      0
      • P pandit84

        Hi , I am having a std map object. I have added key and value in the following order.

        mymap.insert (pair<int,int>(10,20)) ; //Key,value
        mymap.insert (pair<int,int>(5,20)) ;
        mymap.insert (pair<int,int>(104,20)) ;
        mymap.insert (pair<int,int>(1,20)) ;

        But while extracting the data from map I am getting this data in a sorted order (sorted by Key order). How can I prevent this . I am passing the data from this map to a device which needs the data as is from my map. Please help

        S Offline
        S Offline
        Stefan_Lang
        wrote on last edited by
        #3

        If you want to maintain the order of insertion, then you could create a list alongside your map. Each insert will give you an iterator in return, which references the newly inserted element. Store this element in your list, and you're done. If you want to retrieve your elements in order of insertion, use the list. If you want it ordered by key, or just want to retrieve a single object with a key, use your map.

        class MyMap {
        std::map m_map;
        std::list > m_list;
        public:
        typedef std::list >::iterator insert_iterator;
        typedef std::map::iterator key_iterator;
        void insert(int key, int value) {
        m_list.push_back(m_map.insert(pair(key, value)));
        }
        insert_iterator ibegin() { return m_list.begin(); }
        insert_iterator iend() { return m_list.end(); }
        key_iterator kbegin() { return m_map.begin(); }
        key_iterator kend() { return m_map.end(); }
        };

        Note that you will have to dereference your insert_iterator twice: once to get the list element which is an iterator to the map, and a second time to get the actual element of the map:

        MyMap mymap;
        mymap.insert(5, 15);
        mymap.insert(100, 20);
        mymap.insert(34, 30);
        for (MyMap::key_iterator kit = mymap.kbegin(); kit != mymap.kend(); ++kit)
        std::cout << (*kit).second << std::endl; // prints 15 30 20
        for (MyMap::insert_iterator it = mymap.ibegin(); it != mymap.iend(); ++it)
        std::cout << (*(*it)).second << std::endl; // prints 15 20 30

        Of course, this mechanism breaks down once you start erasing elements from your map - the moment you do this, the accompanying iterators stored in the list will be invalidated! You could of course search for these elements and remove them from the list, but that would be very inefficient and might negate the efficiency that you gained from using a map in the first place. In that case you might be better off to just use a list without a map, and sequentially search for the key when you want to retrieve a particular element. Note that you can use the std::find() methods to search for particular elements, you just have to provide a comparator that specifically compares the key. If you have a compiler that supports C++0x you might even use an unnamed lambda to provide the comparator instead of having to define a function object class.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups