Iterator (iterator) and stliterator of STL

Source: Internet
Author: User

Iterator (iterator) and stliterator of STL
3. Introduction to the iterator

In addition to using subscript to access elements of a vector object, the standard library also provides the method of accessing elements: Using iterator. An iterator is a data type that checks elements in a container and traverses elements.

Encyclopedia meaning:

Iterator (IteratorIs an object that can be used to traverse part or all of the elements in the container of the standard template library. Each iterator object represents a specific address in the container. The iterator modifies the interface of the regular pointer. The so-called iterator is a conceptual Abstraction: What acts like the iterator can be called the iterator.


1. Container iterator type

Each container type defines its own iterator type, such as vector:

Vector <int>: iterator iter; the variable name is iter, which can read and write elements in the vector.


2. begin and end operations

Each container defines a group of functions named begin and end for returning the iterator. If the container contains elements, the elements returned by begin point to the first element.

Vector <int>: iterator iter = v. begin ();

If v is not empty, iter points to v [0].

The iterator returned by end points to the next one of the last element and does not exist. If v is null, the return value of begin is the same as that of end.


* Iter; return the reference of the element referred to by iter.

++ Iter moves the iterator forward one position (opposite-iter)

= And! = Operator to compare two iterators. If the two iterators point to the same element, they are equal; otherwise, they do not want to wait.

Example:

String s ("some string"); if (s. begin ()! = S. end) // ensure that s is not empty {auto it = s. begin (); // it indicates the first character of s * it = toupper (* it); // change to uppercase}


We determine that a type is an iterator. If it only supports one set of operations, this operation enables us to access the elements of the container or move from one element to another.


The iterator cannot return the following code as a class pointer:

The iterator of vector <> is not necessarily implemented by common pointers. For example, the iterator of vector in Microsoft STL is a class.
To return a pointer, you can use return & (* point );

But this is ugly, and you expose the internal data of elementList in the Customer Code, destroying the encapsulation.

It is best to return the DcmElement itself. If the cost is too high, the const DcmElement reference or pointer will be returned.

The latter is still prone to suspension of the handle.

Output iterator related to STL iterator

Template <class T>
Void Matrix_ B <T>: PrintMatrix (char * pHeader)
{
...
Fout <pHeader <endl;
Vector <T> >:: iterator it = pMatrix. begin ();
While (it! = PMatrix. end ())
{
Vector <T>: iterator itSec = it-> begin ();
While (itSec! = It-> end ())
{
Fout <* itSec <"";
++ ItSec;
}
Fout <endl;
++ It;
}
Getchar ();
}
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.