4

Possible Duplicate:
comparing iterators from different containers

In practice, std::vector<T>::iterator is probably implemented as a wrapped T* on most STL implementations, so every iterator is associated with a unique memory address (assuming the iterator comes from a non-empty vector).

However, that's an implementation detail. Is there any actual guarantee from the C++ standard that every vector iterator is somehow unique? Specifically, can the end() iterator of one non-empty vector ever equal the end() iterator of another non-empty vector?

For example:

std::vector<int> v1;
std::vector<int> v2;
/* Fill both vectors with values... */

assert(v1.end() != v2.end()); // Does C++ guarantee this assertion will succeed?
Community
  • 1
  • 1
Channel72
  • 24,139
  • 32
  • 108
  • 180

1 Answers1

1

Vectors can't overlap, so the end of one non-empty container cannot easily be the end of another non-empty container. The end of one vector could conceivably be equal to the end of another empty vector. But I don't think you are allowed to compare iterators into different containers, so that doesn't matter much.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
  • It would be possible to have a special value for `end()` for every `vector` (but unlikely since the iterator could not be a raw pointer then), but like you said, it's undefined behavior to find out. – Jeremiah Willcock Feb 26 '11 at 19:30