STL Learning Notes (fifth. STL components)

Source: Internet
Author: User

STL Components

A number of carefully sketched components work together to build the foundation of the STL. The most critical of these components are containers, iterators, and algorithms.

Demonstrates the collaboration between STL components

Container (Containers)

A container Class (container) is used to manage a set of elements. To accommodate different needs, STL provides different types of containers.

In general, containers can be divided into two categories:

1. The Sequence container sequence containers, which is a serializable cluster where each element has a fixed position (depending on the insertion time and location, regardless of the element value).

STL provides three well-defined sequence containers: vector, deque, and list.

2. Associative container associative containers, this is an ordered cluster, the element location depends on the specific sorting criteria.

The STL provides four associative containers: set, Multiset, map, and Multimap.

The specific operation of the container will be demonstrated in the sixth chapter.

Iterators (Iterator)

An iterator is an object that iterates through all or part of the elements in an STL container. An iterator is used to indicate a specific position in the container. The basic operations are as follows:

Operator *

Returns the element value at the current position. If the element has members, you can take them directly with operator->.

Operator + +

Advances the iterator to the next element. Most iterators can also use operator--to bounce back to the previous element.

Operator = = and Operator! =

Determines whether two iterators point to the same location.

Operator =

Assigns a value to the iterator (assigning the position of the element it refers to).

All container classes provide some member functions that allow us to get iterators and iterate through all the elements. The most important of these functions are:

Begin ()

Returns an iterator that points to the start point of the container, which is the position of the first element, if any.

End ()

Returns an iterator that points to the container sleepover. The end point is after the last element.

Iterator classification (Iterator Categories)

All the containers that are pre-defined by the STL are the following two types of iterators:

1. Bidirectional iterator (bidirectional iterator)

Bidirectional iterators can travel in two directions: forward with an ascending operation or backward with a decrement operation. List, set, Multiset, map, and Multimap are the same types of iterators that are provided by these container locks.

2. Random storage iterator (Randomize access iterator)

The random access iterator not only has all the properties of the bidirectional iterator, but also has the random access ability. That is, you can add or decrease an offset to the iterator, handle the distance between iterators, and use the < and > Tears operators to compare two iterators.

The iterators provided by the vector, deque, and strings locks belong to this class.

Algorithm (algorithm)

In order to deal with the elements within the container, STL provides some standard algorithms, including search, sorting, copying, reordering, modifying, numerical operations, and other very basic and common algorithms.

The algorithm is not a member function of a container class, but rather a global function that is used with iterators.

copy function (Functors)

Functor (functor) is the use of a class that looks like a function. Its implementation is to implement a operator () in a class, and this class has a function-like behavior, which is a pseudo-function class.

For example, we define a class:

class x{    public:        returnoperatorconst;        ...  };

You can then call this class of objects as function calls

X fo;...fo (arg1,arg2)   // equivalent to Fo.operator () (ARG1,ARG2);

STL Learning Notes (fifth. STL components)

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.