Thread: iterate through keys of map

  1. #1
    Registered User lord's Avatar
    Join Date
    Dec 2006
    Posts
    61

    iterate through keys of map

    Is there anyway to iterate through the keys of a map without having to go through each of the keys values first?

    Or is there a way to jump to the next key?

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Why not just use iterators?

  3. #3
    Registered User lord's Avatar
    Join Date
    Dec 2006
    Posts
    61
    That's what I am trying to do. But I can't figure out how to iterate through the maps keys without going through its values. For example,
    Code:
    keys:     values:
    a           1 2 3
    b           4 5 
    c           6 7 8 9 10
    d           11
    I want to iterate through a, b, c, d because at this point I don't care about the values.

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Why not simply use find()?

  5. #5
    Registered User lord's Avatar
    Join Date
    Dec 2006
    Posts
    61
    I don't know what the values of the keys are. They can be random characters.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I'm guessing you mean multimap. There is such a thing as equal_range, which gives you an iterator range, but perhaps even better would be upper_bound, which returns an iterator to the first element with a key greater than the one you've got.

  7. #7
    Registered User lord's Avatar
    Join Date
    Dec 2006
    Posts
    61
    perfect... thanks for that

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by lord View Post
    Is there anyway to iterate through the keys of a map without having to go through each of the keys values first?

    Or is there a way to jump to the next key?
    Just iterate over the values, which are std:: pairs of key/value. You only care about the key, so you're looking at iter->first and ignoring iter->second. In the past I implemented an iterator adaptor which would produce only the keys when iterated but it didn't add enough extra convenience for the complexity, so I just do this:

    Code:
    for(map<key, value>::const_iterator i = theMap.begin(); i != theMap.end(); ++i)
    {
        key k = i->first;
        // ...
    }
    Last edited by brewbuck; 10-30-2008 at 10:37 AM.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  2. Creating a map engine.
    By suzakugaiden in forum Game Programming
    Replies: 11
    Last Post: 06-21-2005, 05:06 AM
  3. STL map comparing keys
    By bennyandthejets in forum C++ Programming
    Replies: 4
    Last Post: 06-29-2004, 10:32 AM
  4. Keys of a map
    By nickname_changed in forum C++ Programming
    Replies: 4
    Last Post: 07-10-2003, 04:46 AM
  5. Searching STL Map Inside STL Map Object :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 11-14-2002, 09:11 AM