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?
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?
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,
I want to iterate through a, b, c, d because at this point I don't care about the values.Code:keys: values: a 1 2 3 b 4 5 c 6 7 8 9 10 d 11
I don't know what the values of the keys are. They can be random characters.
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.
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); //}