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
Post a Comment