开发者

Is it valid to compare iterators which are got from the container separately?

开发者 https://www.devze.com 2023-01-07 23:24 出处:网络
Fo开发者_开发知识库r example, it this expression valid in semantic? container.begin() == container.begin();

Fo开发者_开发知识库r example, it this expression valid in semantic?

container.begin() == container.begin();


Yes, so long as neither iterator has been invalidated.

For example, the following would not be valid:

std::deque<int> d;

std::deque<int> begin1 = d.begin();
d.push_front(42);                   // invalidates begin1!
std::deque<int> begin2 = d.begin();
assert(begin1 == begin2);           // wrong; you can't use begin1 anymore.


Yes, begin() will return the same iterator given a container instance, unless you change the container in some way (end() has this property as well). For example, std::vector::push_back() may cause the array to be reallocated to accommodate new elements.

0

精彩评论

暂无评论...
验证码 换一张
取 消