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

  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
    28,413
    No, you need to keep track of the insertion order yourself.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    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