I need to iterate through an std::map...

This is a discussion on I need to iterate through an std::map... within the C++ Programming forums, part of the General Programming Boards category; I need to iterate through an std::map's elements in the same order that they were inserted into the map. Can ...

  1. #1
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Question I need to iterate through an std::map...

    I need to iterate through an std::map's elements in the same order that they were inserted into the map. Can it be done? If so, can anyone post a code example of it in action?

    Thanks in advance.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    No, you need to keep track of the insertion order yourself.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by laserlight View Post
    No, you need to keep track of the insertion order yourself.
    How would I do that?

    EDIT: Nevermind. Solved the problem using a std::vector instead.
    Last edited by Programmer_P; 03-26-2011 at 11:48 AM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,667
    A vector of map iterators, perhaps. You have to keep the vector up to date as well, as operations on the map can invalidate iterators, including and not limited to removing pairs.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Access violation in std::map
    By Dark_Phoenix in forum C++ Programming
    Replies: 7
    Last Post: 08-16-2009, 12:02 PM
  2. store in std::map all kind of structs
    By umen242 in forum C++ Programming
    Replies: 7
    Last Post: 10-19-2008, 02:24 AM
  3. ::iterate
    By Coding in forum C++ Programming
    Replies: 2
    Last Post: 10-04-2008, 05:46 AM
  4. std::map question.
    By Raigne in forum C++ Programming
    Replies: 7
    Last Post: 03-19-2008, 12:29 PM
  5. passing std::map from one dll to another
    By Carlos in forum Windows Programming
    Replies: 2
    Last Post: 05-16-2003, 07:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21