c++ - Wrapping a map of map iterator -


i have c++ class wraps map<int, map<int, stuff>>, find more efficient map<pair<int, int>, stuff>. however, i'd iterator interface of latter style.

for example, i'd able following:

for (const auto& loc_to_stuff : instance) {   pair<int, int> loc = loc_to_stuff.first;   int src = loc.first;   int dst = loc.second;   ... } 

i'm not sure c++ provides here: have implement iterator.

keep in mind iterator class particular members. iterator class operator *, ->, ==, !=, , ++, , maybe few more if can go and/or random seeks.

internally, iterator hold 2 iterators map, map<int, map<int, stuff>>::iterator (the "outer" iterator) , map<int, stuff>::iterator (the "inner" iterator).

++ attempt increment inner iterator, , if fails, increment outer iterator, , start inner iterator @ map outer iterator pointing too. like:

iterator &operator ++ () {     ++m_inner;     // if we've reached end of inner iterator,     // find next non-empty map, , start iterating through it.     // stop if read end of outer map.     // (we're pointing end() of whole thing._     while(m_inner == m_outer->end() && m_outer != m_outer_end) {         ++m_outer;         m_inner = m_outer->begin();     } } 

(the loop skipping empty maps. note you'll need similar loop in begin implementation.)

on main class, need implementations of begin, end (both const , non-const).

the other operators trivial once understand iterators normal objects.

hopefully, can see why might bit of code deal with: might consider of comments suggestions using other map type (map<pair<int, int>, stuff>) let typedef map<pair<int, int>, stuff>::iterator iterator.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -