C++stl Introduction

Source: Internet
Author: User

This article is only a personal learning process in conjunction with the online blog, the collation of STL, is only a brief introduction. Learn notes for individuals only.

introduction of STL (excerpt from: Chenguang (morning))

STL, the standard Template Library, is a highly efficient C + + library with industrial strength. It is housed in the C + + standard library, which is the newest and most revolutionary part of the Ansi/iso C + + standard. The library contains many basic data structures and basic algorithms commonly used in computer science. For the vast number of C + + programmers to provide a scalable application framework, highly reflects the reusability of software. This behavior is somewhat similar to MFC (Microsoft Foundation Class Library) in Microsoft Visual C + + or VCL in Borland C + + Builder (visual Component Library), for the two, we must not be unfamiliar with it.

From the logical level, the concept of generic programming is embodied in STL (generic programming), which introduces many new nouns, such as requirements (requirements), concepts (concept), model, Container (container), algorithm (ALGORITHMN), Iteration sub (iterator), etc. As with the polymorphism (polymorphism) in OOP (object-oriented programming), generics are also a reusable technique for software.

From the implementation level, the entire STL is implemented in a type-parameterized (type parameterized) way, based on a language feature that did not appear in the previous C + + standard-template. If you look at any one version of the STL source code, you will find that the template as the cornerstone of the entire STL is a true thing. In addition, there are many new features of C + + that provide convenience for STL implementations.

Second, STL content

Six components of the STL:
1. Container (Container)

is a data structure, such as List,vector, and Deques, which is provided as a template class method. To access the data in the container, you can use an iterator that is output by the container class;

Containers are divided into: sequential containers and associated containers, container adapters (stack,queue,priority queue), bit sets (Bit_set), string bundles (string_package), and so on.

2, iterators (Iterator)

Provides methods for accessing objects in a container. For example, you can use a pair of iterators to specify a range of objects in a list or vector. An iterator is like a pointer. In fact, C + + pointers are also an iterator. However, iterators can also be class objects that define operator* () and other operator-like methods that are similar to pointers;

The Iterator (iterator) mode, also known as the cursor pattern, provides a way to sequentially access individual elements of an aggregated object without exposing the object's internal representation. Or it might be easier to understand: the iterator pattern is a pattern applied to the aggregation object, which allows us to access the individual elements of the aggregation object in a certain order (the method provided by iterator) without knowing the inner representation of the object.

The role of iterators: can let the iterator and the algorithm do not interfere with each other, and finally can be bonded together without clearance, overloading the *,++,==,! The =,= operator. To manipulate complex data structures, containers provide iterators, algorithms use iterators, and some common types of iterators: iterator, Const_iterator, Reverse_iterator, and Const_reverse_iterator,

More detailed usage can be found in: lhx060288 's blog post.
3. Algorithm (algorithm)

is a template function that is used to manipulate the data in the container.

For example, the STL uses sort () to sort the data in a vector, search for an object in a list with find (), and the function itself is independent of the structure and type of data they manipulate, so they can be used on any data structure from a simple array to a highly complex container;

More detailed algorithm reference: Chenzhongzhou's blog post.
4. Functor (Function object)

A functor (functor), also known as a function object, is actually a struct that overloads the () operator.

1) An imitation function is not a function, it is a class;
2) The Copy function overloads the () operator so that it's pair you can call it like a function (the form of the code appears to be calling

Usefulness of the functor (excerpt from: Gaojun)
Both object and function pointers can be passed as arguments or stored as variables. So we can pass an imitation function to a function that calls the functor as needed (a bit like a callback).
In the STL Template Library, this technique is used extensively to realize the "flexibility" of the library. For_each, its source code is roughly as follows:

1 template< TypeName Iterator, TypeName Functor >2void  for_each (Iterator begin, Iterator end, fucntor func)3{4for  (; begin!=end; begin++ ) 5  func (*begin); 6 }  

This for loop iterates through each element in the container, invokes the functor Func for each element, and implements the idea of programming "do the same thing for every element". In particular, if an affine function is an object, this object can have member variables, which allows the functor to have "state", thus achieving greater flexibility.

5. Iterative Adapter (Adaptor)

The STL provides three container adapters: queue, priority_queue, stack. These adapters are wrappers that wrap a sequence container in a vector, list, deque. Note: The adapter does not provide an iterator, nor can you insert or delete multiple elements at the same time. The individual adapters are summarized below.
6. Space Generator (allocator)

The main work includes two parts 1. Object creation and destruction 2. Memory acquisition and release

See: xy913741894, Huashan big bro

REF:

Http://www.cnblogs.com/shiyangxt/archive/2008/09/11/1289493.html#top

C++stl Introduction

Related Article

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.