What is singular and non-singular values in the context of STL iterators?

前端 未结 4 614
北海茫月
北海茫月 2020-12-06 05:54

The section §24.1/5 from the C++ Standard (2003) reads,

Just as a regular pointer to an array guarantees that there is a pointer value pointing pa

4条回答
  •  离开以前
    2020-12-06 06:50

    Have a look at What is an iterator's default value?.

    As the quote indicates, singular values are iterator values that are not associated with any container. A singular value is almost useless: you can't advance it, dereference it, etc. One way (the only way?) of getting a singular iterator is by not initializing it, as shown in templatetypedef's answer.

    One of the useful things you can do with a singular iterator, is assign it a non-singular value. When you do that you can do whatever else you want with it.

    The non-singular values are, almost by definition, iterator values that are associated with a container. This answers why dereferenceable values are always non-singular: iterators that do not point to any container cannot be dereferenced (what element would this return?).

    As Matthieu M. correctly noted, non-singular values may still be non-dereferenceable. An example is the past-the-end iterator (obtainable by calling container.end()): it is associated with a container, but still cannot be referenced.

    I can't say where these terms are defined. However, Google has this to say about "define: singular" (among other definitions):

    remarkable: unusual or striking
    

    I suppose this can explain the terminology.

提交回复
热议问题