Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this a singular iterator and, if so, can I compare it to another one?

I always thought that a "singular" iterator was one that has been default-initialised, and these could serve as comparable sentinel values of sorts:

typedef std::vector<Elem>::iterator I;
I start = I();

std::vector<Elem> container = foo();

for (I it = container.begin(), end = container.end(); it != end; ++it) {
   if ((start == I()) && bar(it)) {
      // Does something only the first time bar(it) is satisfied

      // ...

      start = it;
   }
}

But this answer suggests not only that my definition of "singular" is wrong, but also that my comparison above is totally illegal.

Is it?

like image 492
Lightness Races in Orbit Avatar asked Jun 19 '13 18:06

Lightness Races in Orbit


People also ask

What is an iterator?

Introduction to Iterators in C++ Difficulty Level : Medium Last Updated : 26 May, 2020 An iterator is an object (like a pointer) that points to an element inside the container.

What is the difference between nonsingular and dereferenceable iterators?

Dereferenceable iterators are always nonsingular, but the converse is not true. For example, a null pointer is nonsingular (there are well defined operations involving null pointers) even thought it is not dereferenceable. An iterator is valid if it is dereferenceable or past-the-end.

What is the difference between iterator and collection?

Below the iterator is the name of an object created by calling iterator () method of collection interface. “collection” is the collection object’s name. Iterators have 4 methods in Java that are used to traverse through collections and retrieve the required information. They are as follows:

Is split iterator a cursor in Java?

Note: SplitIterator can also be considered as a cursor as it is a type of Iterator only. 1. Iterator Iterators in Java are used in the Collection framework to retrieve elements one by one. It is a universal iterator as we can apply it to any Collection object.


1 Answers

Obviously this will work for some iterators - T* being a clear example - but it's definitely not guaranteed correct behavior for all iterators. C++11 24.2.1 [iterator.requirements.general] p5:

Singular values are not associated with any sequence ... Results of most expressions are undefined for singular values; the only exceptions are destroying an iterator that holds a singular value, the assignment of a non-singular value to an iterator that holds a singular value, and, for iterators that satisfy the DefaultConstructible requirements, using a value-initialized iterator as the source of a copy or move operation.

You can replicate your desired behavior with a simple bool flag:

std::vector<Elem> container = foo();
bool did_it_already = false;

for (I it = container.begin(), end = container.end(); it != end; ++it) {
   if (!did_it_already && bar(it)) {
      // Does something only the first time bar(it) is satisfied

      // ...

      did_it_already = true;
   }
}
like image 181
Casey Avatar answered Oct 05 '22 03:10

Casey