Why can two std::vector iterators not be summed?
Asked Answered
A

2

8

This is more of a curiosity question about C++ that came up after learning how to find the index referenced by an iterator.

Given two vector iterators, why can they be subtracted from each other but not added together?

For instance, why does this compile and run:

std::vector<int> vect;
vect.begin() - vect.end();

While this does not:

std::vector<int> vect;
vect.begin() + vect.end();

The question is the get a better concept of how iterators behave. For those that commented and answered, thank you! I need to look more into pointer arithmetic to understand this.

The answer I think helped the most was that iterators are like pointers.

Subtracting two pointers to get the difference in their distance makes sense, just like looking at 1-10 on a number line and wanting the distance between 7 and 3, you subtract 7 from 3 for a distance of 4.

Adding 7 and 3 gives 10, which doesn't help me find the distance between them and, in a container, will end up pointing to something outside the container's bounds, which is not helpful or useful.

Amoy answered 2/9, 2020 at 15:16 Comment(16)
and, in your opinion, what should be the output of that operation? i.e. what would you expect to be the output of iterator addition?Kaceykachina
Well, I'm not sure. I know that iterators begin and end point to the beginning and end of the container respectively, but I can't work out how an iterator minus an iterator gives and int, while an iterator plus an iterator is not a possible operation.Amoy
Exactly the same reason as why they cannot be multiplied or divided - there is no meaningful result of such operations.Woodie
@BrandonManess Subtracting iterators is the same as subtracting pointers. It is the number of elements between the two.Plaster
That's what he's trying to get you to: what would such an operation be for? What would it return, conceptually? (The fact that there's not conceptual outcome for this is the reason it's not defined)Noellenoellyn
@BrandonManess Add this information to your question please. Otherwise it's unclear what you're expecting.Flannelette
@BrandonManess: An iterator minus an iterator is an int, which might be negative. The inversion of that operation is an iterator plus an int, resulting in an iterator. Again, adding two iterators has no intrinsic meaning, so it's not been defined.Skyscape
You may also be able to add an integer type to an iterator. This would advance the iterator to a new position within the structure.Unreadable
Before we all get on our high horses as to why the addition of iterators or pointers is meaningless, note it can have a use if you want to calculate a midpoint, for which the expression (x + y) / 2 is tractable. Alas it needs to be written as x + (y - x) / 2, which is not as clear and relies on second term being an interval.Trainload
Think of an iterator as an address to your home. You are adding your home's address with the address of your friend's home, or the home next door. Adding addresses doesn't make sense.Unreadable
@Bathsheba: But you couldn't do that (legally) with pointers, either. Pointer arithmetics is only defined for pointers into the same array, and there is no guarantee that (x + y) would still be inside that array (or inside addressable space, or not wrapping around the address register)...Skyscape
@DevSolar; Indeed you can't as I hope my comment makes clear. I'm merely reacting to the notion that x + y is always meaningless. It isn't. You do make a very important point about pointer arithmetic being only valid within arrays (and 1 past the end).Trainload
@ThomasMatthews You mean nobody lives at 247 Main Street Main Street? In comparison, 124 Main Street minus 123 Main Street is just 1.Emeric
@user253751 I don't understand. I mean what's the result when adding 42 Main Street to 137 Main Street? The difference between two addresses is a distance value.Unreadable
Reading this might help: en.cppreference.com/w/cpp/iterator/distanceZasuwa
A difference of 2 N-dimensional points is an N-dimensional vector. A sum of 2 N-dimensional points is ???. math.stackexchange.com/questions/2555570/…Rocha
B
15

Iterators are modelled after pointers.

Given pointers P1 and P2, the expression P2 - P1 gives you the offset/distance between the pointers. There is nothing sensible you can expect from the expression P1 + P2. Extend that idea to iterators and you will understand why subtraction between two iterators makes sense but addition does not.

Burse answered 2/9, 2020 at 15:22 Comment(3)
Thanks Sahu! I need to look more into pointers to really grasp the concept. "the expression P2 - P1 gives you the offset between the pointers" this wording helped a ton.Amoy
I'd rather call it the distance rather than the offset. But that point is the reason why subtraction makes sense while addition does not.Eyestrain
Iterators are not only modelled after pointers, they actually might be pointers. This may not be the case for std::vector, but std::array<T, N>::begin() typically returns a T* to the start of the array.Idoux
S
3

because it makes not much sense to do that operation, therefore there is no operator+ defined in the iterator

Suziesuzuki answered 2/9, 2020 at 15:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.