gilgamash
gilgamash

Reputation: 902

C++ iterator operator definition

any idea or link concerning the "definition" of how operators of iterators have to work? To be more exact: How excatly do, for instance, operators "==" and "!=" have to be implemented -- do they have to compare members of the data they hold (which imho would be a problem, as the iterator should not know about the data in the first place)? How do you compare to .end() and .start()? For pointer iterators: is it just an address check?

Would be glad to hear about your ideas or get a link to the definition.

Upvotes: 0

Views: 1731

Answers (3)

Fred Foo
Fred Foo

Reputation: 363517

The original STL has a design document on iterators.

In short, they are a generalization of pointers, so == should check whether two iterators point at the same item in the same container. Pointer iterators should indeed compare equal when their pointers do.

Comparing to a begin() or rbegin() should be trivial; comparing to an end() or rend() can be done in various ways, like making it an index equal to the size() of a container.

Upvotes: 1

Marcelo Cantos
Marcelo Cantos

Reputation: 185842

The == and != operators aren't expected to compare the contents, they merely check whether two iterators refer to the same object.

Upvotes: 0

Fred Larson
Fred Larson

Reputation: 62053

I think you need to read something like this: http://stdcxx.apache.org/doc/stdlibref/iterators.html

The operators == and != compare the iterators, not the data they refer to.

Upvotes: 4

Related Questions