I couldn't find a better title for this issue, please update it if needed as per the below question.
Consider an iterator - iter
, pointing to elements in a std::vector<int>
. I'm inserting the value of *iter
at the current iterator position using:
iter = target.insert(iter, *iter);
I understand that insert
will return the iterator to the newly inserted element. Now I changed the above statement to:
iter = target.insert(iter, *iter++);
I knew that was going to behave awkwardly, and it resulted in Segmentation fault. What I can't understand is, how the above assignment is evaluated? I mean, to which element will iter
point to after that assignment. As far as I understand, since I've used a post-increment operator, it should behave in similar way as the first statement, which I am inferring from the below sample assignment:
i = i++;
Since the above assignment doesn't affect the value of i
, same should be true in case of *iter++
.
What's the actual behaviour behind the scene?
This is the real code:
std::vector<int>::iterator begin = target.begin();
while (begin != target.end()) {
if (*begin % 2 == 0) {
begin = target.erase(begin);
} else {
begin = target.insert(begin, *begin); // I changed this line
begin += 2;
}
}
Basically the above code is removing the even element from vector, and duplicating the odd element.
Edit:
Ok, if I change the second line to ++begin
, in addition to the previous change - it works in the same way as current code. So, I replaced two lines in the else
block, with these lines:
begin = target.insert(begin, *begin++);
++begin;
So, it seems like it is assigning the iterator one past than that in the previous case.
So, this is what I understood from this behaviour:
*begin++
first de-references thebegin
to get the value at current location.begin
is post-incremented- The
insert
now inserts the de-referenced value before the iterator after the post-increment. So, it's not inserting before the originalbegin
, rather, before the++begin
. And then returns the value++begin
, where it inserted.
Have I interpreted it correctly?