Introduction to Standard Template Library (STL) for C + +

Source: Internet
Author: User

STL, Standard Template Library, is the implementation of C + + for generic programming ideas, which was first developed by HP Labs. This technology has existed for a long time before being introduced to C + +. Later STL became part of the Ansi/iso C + + standard. Each C + + vendor also has its own template library, which may be highly efficient, but portability is not necessarily good.

In the C + + standard, STL is organized into the following 17 header files:<algorithm>, <deque>, <functional>, <iterator>, <array>, <vector>, <list>, <forward_list>, <map>, <unordered_map>, <memory>, <numeric >, <queue>, <set>, <unordered_set>, <stack> and <utility>.

Commonly used are: algorithm (algorithm), container (container), and iterator (iterators). Almost all of the code takes the form of template classes and template functions, which provides a better opportunity for code reuse than traditional libraries that consist of functions and classes.

1) algorithm (algorithm)
STL provides approximately 100 template functions for implementing algorithms, which are mainly composed of file <algorithm>,<numeric> and <functional>.
<algorithm> is the largest of all STL header files, and it is made up of a large number of template functions, which are commonly used to include comparisons, exchanges, lookups, traversal operations, copying, modification, removal, inversion, sorting, merging, and so on.
<numeric> is small and includes only a few template functions for simple mathematical operations.
<functional> defines a number of template classes that declare function objects.

2) container (container) (also known as set collection)
In the actual development process, the importance of the data structure itself is not inferior to the algorithm of manipulating data structures, the choice of data structure becomes more important when there is a high demand for time in the program.
By setting up some template classes, STL containers provide support for the most commonly used data structures, which allow you to simplify many repetitive and tedious tasks by allowing you to specify the data type of the elements in the container.
As the following table:

Data

Implementing the header File

Vectors (vector)

Sequential containers

<vector>

Listing (list)

Sequential containers

<list>

Dual Queue (deque)

Sequential containers

<deque>

Collection (SET)

Associative containers

<set>

Multiple collections (Multiset)

Associative containers

<set>

Stacks (Stack)

Container Adapter

<stack>

Queuing (queue)

Container Adapter

<queue>

Priority Queue (Priority_queue)

Container Adapter

<queue>

Mapping (MAP)

Associative containers

<map>

Multi-mapping (Multimap)

Associative containers

<map>

3) iterator (iterator)
An iterator is a type of data that allows a programmer to examine elements within a container and implement an element traversal. The C + + standard library defines an iterator type for each standard container. Iterator types provide a more generalized approach than subscript operations: All standard library containers define the appropriate iterator types, and only a few containers (such as arrays) support subscript operations. Because iterators are applicable to all containers, modern C + + programs tend to use iterators rather than subscript operations to access container elements.
Iterators are the most basic part of STL, and iterators are used in STL to link algorithms and containers, and act as a glue and agent. Almost all of the algorithms provided by the STL are working through an iterator that accesses the sequence of elements, each of which defines itself as a proprietary iterator to access the elements in the container.
The iterator section mainly consists of the header file <utility>,<iterator> and <memory>. <utility> is a very small header file that includes many of the methods used by iterators in the declaration,<iterator> that run through several templates used in STL, <memory> allocates storage space for elements in the container, Also provides a mechanism for temporary objects that are generated during some algorithm execution the main part of,<memory> is the template class allocator, which is responsible for generating the default allocator in all containers.

Personal understanding: Iterators are well understood, like pointers and array subscripts in C, pointers and array subscripts can be used to iterate over the elements in the arrays, and iterators can traverse the elements in the container.

Introduction to Standard Template Library (STL) for C + +

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.