Navigating maps using iterators in c++ -


i've been having issue using map iterator, i'm hoping here can resolve me.

basically, need return last 10 keys in map have, tried this:

for (map<int, char>::iterator = (singlecountswitched.end()); != (singlecountswitched.end()-10); --i) {                 cout << (*i).first << ": " << (*i).second << endl;             } 

however giving me following error:

main.cpp:150:112: error: no match ‘operator-’ in ‘singlecountswitched.std::map<_key, _tp, _compare, _alloc>::end<int, char, std::less<int>, std::allocator<std::pair<const int, char> > >() - 10’ 

which seems there no support - operator (a similar message thrown when using map.begin()+10). using iterator incorrectly? thought iterators stl containers supposed have overloaded +'s , -'s.

map<int, char>::reverse_iterator r_it_end = singlecountswitched.rbegin(); map<int, char>::reverse_iterator r_it_begin = singlecountswitched.rbegin(); std::advance(r_it_begin, 10); 

you can use advance of normal iterator (size() - 10)

i think better one

map<int, char>::reverse_iterator r_it_begin = singlecountswitched.rbegin(); std::advance(r_it_begin, 10);  (map<int, char>::iterator = r_it_begin.base(); != singlecountswitched.end(); ++it) { } 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -