Click here to Skip to main content
16,017,852 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionDelete a view Pin
john563224-Mar-11 3:03
john563224-Mar-11 3:03 
AnswerRe: Delete a view Pin
Albert Holguin24-Mar-11 4:24
professionalAlbert Holguin24-Mar-11 4:24 
GeneralRe: Delete a view Pin
john563224-Mar-11 19:45
john563224-Mar-11 19:45 
GeneralRe: Delete a view Pin
Albert Holguin25-Mar-11 3:48
professionalAlbert Holguin25-Mar-11 3:48 
AnswerRe: Delete a view Pin
Richard MacCutchan24-Mar-11 5:47
mveRichard MacCutchan24-Mar-11 5:47 
QuestionSTL : Map.Insert always sort the inserted key - How to prevent this Pin
pandit8424-Mar-11 0:35
pandit8424-Mar-11 0:35 
AnswerRe: STL : Map.Insert always sort the inserted key - How to prevent this Pin
Chris Losinger24-Mar-11 1:03
professionalChris Losinger24-Mar-11 1:03 
AnswerRe: STL : Map.Insert always sort the inserted key - How to prevent this Pin
Stefan_Lang30-Mar-11 1:51
Stefan_Lang30-Mar-11 1:51 
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<int,int> m_map;
    std::list<pair<int,int> > m_list;
public:
    typedef std::list<pair<int,int> >::iterator insert_iterator;
    typedef std::map<int,int>::iterator key_iterator;
    void insert(int key, int value) {
        m_list.push_back(m_map.insert(pair<int,int>(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.
QuestionHow to reduce Flickering ? Pin
002comp23-Mar-11 22:47
002comp23-Mar-11 22:47 
AnswerRe: How to reduce Flickering ? Pin
CPallini23-Mar-11 23:00
mveCPallini23-Mar-11 23:00 
GeneralRe: How to reduce Flickering ? Pin
Maximilien24-Mar-11 2:56
Maximilien24-Mar-11 2:56 
GeneralRe: How to reduce Flickering ? Pin
CPallini24-Mar-11 4:27
mveCPallini24-Mar-11 4:27 
AnswerRe: How to reduce Flickering ? [modified] Pin
Cool_Dev24-Mar-11 0:22
Cool_Dev24-Mar-11 0:22 
GeneralRe: How to reduce Flickering ? Pin
Nitheesh George24-Mar-11 2:02
Nitheesh George24-Mar-11 2:02 
GeneralRe: How to reduce Flickering ? Pin
Albert Holguin24-Mar-11 4:26
professionalAlbert Holguin24-Mar-11 4:26 
AnswerRe: How to reduce Flickering ? Pin
Stephen Hewitt24-Mar-11 2:14
Stephen Hewitt24-Mar-11 2:14 
GeneralRe: How to reduce Flickering ? Pin
002comp24-Mar-11 18:47
002comp24-Mar-11 18:47 
QuestionVariable In SQL String Pin
Mike Certini23-Mar-11 19:11
Mike Certini23-Mar-11 19:11 
AnswerRe: Variable In SQL String Pin
«_Superman_»23-Mar-11 19:21
professional«_Superman_»23-Mar-11 19:21 
GeneralRe: Variable In SQL String Pin
Mike Certini23-Mar-11 20:05
Mike Certini23-Mar-11 20:05 
GeneralRe: Variable In SQL String Pin
markkuk23-Mar-11 20:29
markkuk23-Mar-11 20:29 
AnswerRe: Variable In SQL String Pin
Pravin Patil, Mumbai23-Mar-11 22:16
Pravin Patil, Mumbai23-Mar-11 22:16 
QuestionIs there a linkable chat control? Pin
includeh1023-Mar-11 3:48
includeh1023-Mar-11 3:48 
AnswerRe: Is there a linkable chat control? Pin
Hans Dietrich23-Mar-11 5:19
mentorHans Dietrich23-Mar-11 5:19 
QuestionUse Modeless Dialog inside MFC DLL Pin
Andraw Tang22-Mar-11 11:52
Andraw Tang22-Mar-11 11:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.