STL Learning 1: Theoretical Basis of the standard template library,

Source: Internet
Author: User

STL Learning 1: Theoretical Basis of the standard template library,

STL (Standard Template Library) is a collection of software developed by the HP lab. This technology has been around for a long time before it was introduced into C ++.

In a broad sense, STL is divided into three types: algorithm (algorithm), container (container), and iterator (iterator). containers and algorithms can be seamlessly connected through the iterator. Almost all code uses the template class and template function method, which provides a better chance of code reuse than the traditional library composed 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>.

UseSTLBenefits

  • STL is a part of C ++, so you don't need to install anything. It is built into your compiler.
  • An important feature of STL is the separation of data structures and algorithms, making STL very common.
  • Programmers don't have to think about the specific implementation process of STL. As long as they can use STL skillfully, we can focus on other aspects of program development.
  • STL has the advantages of high reusability, high performance, high portability, and cross-platform.

STL details six components

  • -Container)
  • -Algorithm (Algorithm)
  • -Iterator)
  • -Function object)
  • -Adapter)
  • -Space configurator (allocator)

Container

Container concept: it is the implementation of the data structure, used to manage a group of elements.

Container category:

    • Sequence containers each element has a fixed position-it depends on the insertion time and location, and has nothing to do with the element value.Vector, Deque, list
    • The position of the Associated containers element depends on the specific sorting criterion and is irrelevant to the insertion sequence.Set, Multiset, map, multimap
Basic Introduction to containers
Data Structure Description Implement header files
Vector) Elements of continuous Storage <Vector>
List) A two-way linked list composed of nodes. Each node contains an element. <List>
Dual Queue (deque) An array of continuously stored pointers to different elements <Deque>
Set) A red-black tree composed of nodes. Each node contains an element, which is arranged by a certain predicate acting on the element pair. No two different elements can have the same order. <Set>
Multiple sets (multiset) A set of two elements in the same order is allowed. <Set>
Stack) Sort the values of the last, first, and foremost <Stack>
Queue) Sorting of first-in-first-out orders <Queue>
Priority_queue) The order of elements is determined by a certain predicate acting on the stored value pair. <Queue>
Map) A set of {key, value} pairs that act on the predicates of a key pair. <Map>
Multimap) Allow ing of key pairs in equal order <Map>

Iterator

There is a basic principle in software design. All problems can be simplified by introducing an indirect layer, which is completed by the iterator in STL. In summary, the iterator is used in STL to associate the algorithm with the container and acts as a viscosity and agent. Almost all algorithms provided by STL work by accessing Element Sequences through the iterator. Each container defines its own proprietary iterator to access elements in the container. The iterator consists of the header file <utility>, <iterator>, and <memory>.

Algorithm

C ++ allows you to postpone the selection of some types through the template mechanism until you really want to use the template or make the template special, STL uses this to provide many useful algorithms.

The algorithm consists of the header files <algorithm>, <numeric>, and <functional>. <Algorithm> is the largest of all STL header files (though well understood). It is composed of a large number of template functions. It can be considered that each function is independent to a large extent, the commonly used functions include comparison, exchange, search, traversal, replication, modification, removal, reversal, sorting, and merging. <Numeric> the size is very small. It only includes several template functions that perform simple mathematical operations on the sequence, including addition and multiplication operations on the sequence. <Functional> defines some template classes to declare function objects.

Imitation Functions

A class that overload function call operators. Its objects are often called function objects. A Class Object represents a function. It uses a class object through "Object Name + (parameter list)". If there is no context, you can treat it as a function.

Function Adapter

It is used to specialize in and expand mona1 and binary function objects.

Function adapters can be divided into the following four types:

  • Bind an adapter
  • Combination Adapter
  • Pointer function Adapter
  • Member function Adapter

Space Adapter

The main work includes two parts:

  • Object creation and destruction
  • Memory acquisition and release

 

Source: http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_stl_001.html

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.