STL (Standard Template Library) Theory Foundation, container, iterator, algorithm

Source: Internet
Author: User
Tags windows visual

Basic concepts

The STL (Standard Template Library) is a generic term for a range of software developed by HP Labs. It is now mostly in C + +, but the technology has been around for a long time before being introduced to C + +.
STL is broadly divided into three categories: algorithm (algorithm), container (container), and iterator (iterators), and containers and algorithms can be seamlessly connected through 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. In the C + + standard, STL is organized into the following 13 header files:<algorithm>, <deque>, <functional>, <iterator>, <vector>, <list>, <map>, <memory>, <numeric>, <queue>, <set>, <stack> and <utility >.

STL details six components.
– Containers (Container)
– Algorithm (algorithm)
– Iterators (Iterator)
– Functor (function object)
– Adapter (Adaptor)
– Space formulators (allocator)


Benefits of using STL
1) STL is part of C + +, so there is no additional installation, it is built into your compiler.
2) An important feature of STL is the data structure and algorithm separation . Although this is a simple concept, this separation does make the STL very generic.
For example, in an STL vector container, you can put elements, underlying data type variables, the address of an element, and
the sort () function of the STL to manipulate containers such as vector,list.
3) Programmers can not think about the specific implementation process of STL, as long as the ability to use the STL is OK. So that they can focus on other aspects of program development.
4) STL has the advantages of high reusability, high performance, portability, and cross-platform.
High reusability: Almost all of the code in the STL is implemented in the form of template classes and stencil functions, which provides better code reuse opportunities than traditional libraries that consist of functions and classes.
High performance: such as map can efficiently find the specified record from 100,000 records, because map is implemented with a variant of the red-black tree. (The red and black trees are a kind of flat horizontal binary tree)
High Portability: A module written in STL on Project A can be ported directly to Project B.
Cross-platform: code written in Windows Visual Studio can be compiled directly on the Mac OS Xcode.
5) Programmers can not think about the specific implementation process of STL, as long as the ability to use the STL is OK. So that they can focus on other aspects of program development.
6) Knowing these benefits of STL, we know that STL is undoubtedly the most proud part of C + + programmers. Every C + + programmer should learn the STL well. Only programmers who are proficient in using STL are good C + + programmers.
7) In short: recruitment work, often encountered C + + programmers are not very familiar with STL. Most of them have an approximate image, and they are quite dazed with which containers and algorithms should be used under what circumstances. STL is an essential basic skill for C + + programmers, and mastering it is a great benefit for improving C + + programming.

container

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.
The number of classic data structures is limited, but we often repeat some of the code that is written to implement the structure of vectors, lists, and so on, which are very similar to each other in order to accommodate changes in the data. STL containers provide us with the convenience of allowing us to reuse existing implementations to construct our own data structures under a particular type, by setting up templates that enable STL containers to support the most commonly used data structures that allow us to specify the data type of the elements in the container. Can simplify many of our repetitive and tedious work.
The container part mainly consists of the file <vector>,<list>,<deque>,<set>,<map>,<stack> and <queue>. For some commonly used containers and container adapters (which can be thought of as containers implemented by other containers), the following table summarizes the correspondence between them and the corresponding header files.

The concept of containers

Used to manage a set of elements



Classification of containers

Sequence container (Sequence containers)
Each element has a fixed position-depending on the insertion time and location, regardless of the element value.
Vector, deque, list
Associative containers (associated containers)
The position of the element depends on the specific sorting criteria, regardless of the insertion order
Set, Multiset, map, Multimap

Data

Describe

Implementing the header File

Vectors (vector)

Elements that are continuously stored

<vector>

Listing (list)

A doubly linked list of nodes with one element per node

<list>

Dual Queue (deque)

An array of continuously stored pointers to different elements

<deque>

Collection (SET)

A red-black tree of nodes, each of which contains an element that is arranged between nodes in a predicate that acts on the pair of elements, and that no two different elements can have the same order

<set>

Multiple collections (Multiset)

Allows a collection of two elements with equal order

<set>

Stacks (Stack)

Arrangement of last-in-first-out values

<stack>

Queuing (queue)

The arrangement of FIFO

<queue>

Priority Queue (Priority_queue)

The order of the elements is a queue that is determined by a predicate acting on the stored value pair

<queue>

Mapping (MAP)

A set consisting of {key, value} pairs, arranged in a predicate on a key pair of functions

<map>

Multi-mapping (Multimap)

Allow mappings of key pairs to have equal order

<map>


Iterators

Iterators are the most basic part of the work, but they are more difficult to understand than the first two. The basic principle of software design is that all problems can be simplified by introducing an indirect layer, which is done with an iterator in the STL. In summary, iterators are used in STL to link algorithms and containers, and act as a binder. 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, and the description of <memory> is very difficult, It allocates storage space in an unusual way for elements in a container, and also provides a mechanism for temporary objects that are generated during some algorithm execution,<memory> the main part of the template class is allocator, which is responsible for generating the default allocator in all containers.

Algorithm

The library's choice of data types plays a key role in its reusability. For example, a square root function is certainly more reusable in the case of using a floating-point number as its parameter type than using an integer as its parameter class. The mechanism of C + + through templates allows you to defer certain types of choices until you really want to use a template or to make a template special, and the STL provides quite a few useful algorithms. It accomplishes these algorithms in an effective framework--you can divide all types into a few categories, and then you can replace other types in the same category with one type in the template's parameters.
The STL provides about 100 template functions for implementing algorithms, such as the algorithm for_each invokes the specified function for each element in the specified sequence, stable_sort the sequence with the rules you specify, and so on. In this way, as long as you are familiar with the STL, many of the code can be greatly reduced, only by invoking one or two algorithm templates, you can complete the required functions and greatly improve efficiency.
The algorithm part mainly consists of the file <algorithm>,<numeric> and <functional>. <algorithm> is the largest of all STL header files (although it is well understood), it is composed of a large number of template functions, it can be considered that each function is largely independent, which commonly used in the scope of functions related to the comparison, Exchange, find, traverse operation, copy, modify, Remove, invert, sort, merge, and so on. The <numeric> volume is small, and includes only a few template functions that perform simple mathematical operations on the sequence, including some operations on the sequence of addition and multiplication. <functional> defines a number of template classes that declare function objects.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

STL (Standard Template Library) Theory Foundation, container, iterator, algorithm

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.