Matt Matt - 7 months ago 41
C++ Question

Does each element in a vector have a unique memory address?

Fairly new to C++. Referring to this snippet of code:

for (std::vector<int>::iterator it = myvector.begin() ; it != myvector.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';

In particular when initializing the iterator and the end condition. Does each element in the vector have a unique memory address to point to? Or do iterators work differently than pointers?


Although iterators are not necessarily pointers (although for std::vectors most non-debug implementations implement them as thin wrappers around pointers), std::vector is guaranteed to have its elements contiguous in memory. Which implies that YES, every element has a distinct memory location.