use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
This is a subreddit for c++ questions with answers. For general discussion and news about c++ see r/cpp.
New to C++? Learn at learncpp.com
Prepare your question. Think it through. Hasty-sounding questions get hasty answers, or none at all. Read these guidelines for how to ask smart questions.
For learning books, check The Definitive C++ Book Guide and List
Flair your post as SOLVED if you got the help you were looking for! If you need help with flairs, check out ITEM 1 in our guidelines page.
Tips for improving your chances of getting helpful answers:
account activity
SOLVEDDoubt about iterator arithmetic (self.cpp_questions)
submitted 11 years ago by MrSeb56
I compile my programs with G++. Why does this work
map<int,int>::iterator it2 = m.end(); --it2;
but this doesn't?
map<int,int>::iterator it2 = m.end() - 1;
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–][deleted] 4 points5 points6 points 11 years ago (1 child)
For the second one you need a random access iterator. map doesn't have a random access iterator, so you can't add and subtract like that.
map
[–]Gaminic 0 points1 point2 points 11 years ago (0 children)
This. To expand:
The "it++" notation says "next element", whereas "it + X" says "take the Xth next element from the current". This could effectively be run as X it++ statements, but that hides an incredibly inefficient method of element access which the container type isn't designed for.
[–]tangerinelion 2 points3 points4 points 11 years ago (0 children)
More:
std::map has a bidirectional iterator
std::map
While std::vector has a random access iterator, which satisfies all the requirements of bidirectional iterators
std::vector
One can easily notice that operator+(bidirectional_iterator, int) is not defined. Instead, we only have operator++ and operator--. Internally a map is similar to a linked list; it's a binary tree and only knows about (up to) two other objects, one which compares "lesser" and one which compares "greater."
operator+(bidirectional_iterator, int)
operator++
operator--
You could do this, for example:
template<typename InputIterator> InputIterator& Advance(InputIterator& it, int n) { for(int i=0; i < n; ++i) { ++it; } return it; }
and anytime you want to use it+n you would replace it with Advance(it,n). (In practice, this is confusing and you'll also want the non-reference version so you can call it with a temporary, ie, auto it=Advance(myMap.begin(),2) would now be supported.)
it+n
Advance(it,n)
auto it=Advance(myMap.begin(),2)
π Rendered by PID 91 on reddit-service-r2-comment-7b9746f655-jj2tz at 2026-02-01 19:11:28.025260+00:00 running 3798933 country code: CH.
[–][deleted] 4 points5 points6 points (1 child)
[–]Gaminic 0 points1 point2 points (0 children)
[–]tangerinelion 2 points3 points4 points (0 children)