Moving to the For Beginners forum.
In keeping with For Beginners, I'll start with the direct answer: The easiest way is to take the address of an item is the indexed array form like : &myVector[0];
to get the address of item 0, or &myVector[n]
for the address of item n
.
For more on the subject:
It looks like you're learning about iterator invalidation. It's an essential subject to master.
It is critical to understand object lifetimes in software development. Objects are created, moved, and destroyed in specific ways, and if you break the rules around them you'll introduce bugs in the software. Here is a table with the simplified invalidation rules for the various standard library containers.
There are also tools out there like “smart pointers”, shared pointers are a way to distribute object ownership, weak pointers allow code to point to objects which can potentially have been destroyed so you can write code to safely handle it, unique pointers ensure only one copy of an object exists. Each have uses in managing object lifetimes. They solve a lot of problems around advanced object management and game engines do a lot of work around managing smart pointers.
And finally, you've got some example attempts where you are using iterators. Iterators are potentially complex objects, very powerful but the complexity can overwhelm beginners. It gets more complicated with iterators as there are some like output iterators that aren't meant for dereference, and there are the concept of indirectly readable and indirectly writable iterators. Dereferncing an iterator can also have concerns around chaining, as operator* and operator-> have potentially different behavior. In your example you'd get the address you asked about from simple dereference with *it
but for more complex types that might get you something you didn't quite expect, like the address of an intermediate object in a chain.