C + + Proverbs: Using feature classes for type information

Source: Internet
Author: User

STL is mainly composed of containers (container), iterators (iterator) and Algorithms (algorithm) templates (templates), but there are several utility templates (utility templates). One of them is called advance. Advance moves a specified iterator (iterator) to a specified distance:

template<typename IterT, typename DistT> // move iter d units
void advance(IterT& iter, DistT d); // forward; if d < 0,
// move iter backward

Conceptually, advance is only doing iter = d, but advance cannot do this because only random access iterators (random access iterator) supports + = operation. The iterator (iterator) type is forced to implement advance by repeatedly using + + or-D times.

Don't you remember the STL iterator categories (iterator type)? No problem, let's do a simple review. There are five kinds of iterators (iterators) corresponding to the operations they support. Input iterators can only move forward, only one step at a time, only read what they point to, and read only once. They are based on the read pointer (reading pointer) in an input file, and the istream_iterators in the C + + library is the typical representative of this kind. Output iterators is similar to this, except for output: they can only move forward, only one step at a time, only write what they point to, and only write once. They are based on write pointer (write pointers) in an output file; Ostream_iterators is a typical representative of this kind. This is the two least powerful iterator categories (iterator type). Because input (input) and output iterators (outputs iterators) can only move forward and can only read or write where they point at most once, they are only suitable for one-pass operations.

A more powerful iterator category (iterator type) is the forward iterators (forward iterator). This iterators (iterator) can do everything that input (input) and output iterators can do, plus they can read or write what they point at more than once. This allows them to be used for multi-pass operations. The STL does not provide a singly linked list (one-way list), but some libraries provide (often referred to as slist), and the iterators (iterator) of this containers (container) is forward iterators (forward iterator). The iterators (iterators) of the TR1 hashed containers (hash container) can also belong to forward category (forward iterators).

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.