Wednesday, 15 September 2010

c++ - Prefer Iterators Over Pointers? -


There is a collision of a question, in which there was a comment, but it was removed as part of the collision.

For people who are not able to see deleted posts, comments use my const char * instead of string :: const_iterator s It is at: "Iterators may be a better way to go get-go, because it appears that your pointers are treated properly."

So my question is, iterators Holds an internal value greater than string :: const_iterator const char * , such as my answer to string :: Does comp_iterators understand ?

intro

There are many benefits to using iterators instead of pointers, From:

  • Rebation vs debug , and;
  • Improved type-protection, and;
  • In making this possible, different code-paths can be written for general code (iterators can be e-created, such as a linked list, to work with any data-structure , Whereas internal signals are very limited in this regard).

  • debugging

    Because, among other things, by deferring a mutant at the end of a range, undefined behavior , in such a case, an implementation is independent - including raising the diagnosis, that you are doing something wrong. The standard library implementation provided by

    GCC detects some mistake (if enabled) then the issue of diagnosis will be.

    Example

      #define _GLIBCXX_DEBUG 1 / * Enable debug mode * / # Lt; Vector & gt; # Include & lt; Iostream & gt; Int main (int argc, char * argv []) {std :: vector & lt; Int & gt; V1 {1,2,3}; For (auto it = v1.begin (); ++ it) std :: cout & lt; & Lt; this *; }  
      / usr / contains / c ++ / 4.9.2 / debug / safe_iterator.h: 261: error: attempt Variation to include last-the-end iterator objects in operation: Iterator "this" @ 0x0x7fff828696e0 {type = N11__gnu_debug14_Safe_iteratorIN9__gnu_cxx17__normal_iteratorIPiNSt9__cxx19986vectorIiSaIiEEEEENSt7__debug6vectorIiS6_EEEE (variable iterator); State = east-to-end;  

    If we were not working with the sign , no matter if we debug mode

    If we do not enable debug mode for libstdc ++ , then another displayable version (without extra bookkeeping method) ) Implementation will be used - and no diagnosis will be issued.



    (potential) Better type protection

    Since actual iterators , It can be used to increase type-protection - but you have to see the documents of your implementation to see if this is the case.


    Example:

      #include & lt; Vector & gt;  

      struct A {}; Structure B: A {};  

      // - of // v it_func (std :: vector & lt; zero; p & Gt; :: iterator Beg, std :: vector & lt; a & gt; :: Iterator end); Zero ptr_func (B * beg, A * end); // ^ - OP  

      int main (int argc, char * argv []) {std :: vector & Lt; P & gt; V1; It_func (v1.begin (), v1.end ()); // (A) ptr_func (v1.data (), v1.data () + v1.size ()); // (b)}  

    collaboration

    • ( A) , based on implementation, std :: vector & lt; A & gt; :: iterator and std :: vector & lt; B & gt; There may be a compile-time error since ::. Iterator is not likely to be of the same type.
    • (b) will always compile, because B * to A * .

1 comment: