STL Learning Series IV: summary of STL Learning

Source: Internet
Author: User
Tags constant header numeric set set

Providing a type-safe, efficient and Easy-to-use STL is undoubtedly the proudest part of the C + + programmer. Every C + + programmer should study the STL well:.

STL (Standard Template Library Standard template base) is an important part of the C + + standard library, which was first developed by Stepanov and Lee, and it was developed almost simultaneously with C + +; At the beginning the STL chose Ada as the implementing language, But the ADA is a bit disappointing, finally they chose C + +, a reason, C + + already have a template. Later, the STL was added to the C + + library. 1996, Hewlett-Packard Company also released the STL free of charge, for the promotion of STL made a great contribution.

The STL generally includes the container (container), the algorithm (algorithm) and the iterator (iterator), and the container and the algorithm can be seamlessly connected through the iterator.

STL embodies the idea of paradigm-type programming, it has high reusability, high performance, high portability. Programmers do not have to think about the specific implementation process, as long as the skilled application is OK. (Interested in the specific implementation, you can see Houtie Teacher's "STL Source analysis") so they can focus on the other aspects of program development.

I really admire the computer and math elites who created the STL. Because they do a very hard thing--abstract concept. The STL, by abstracting the container into a unified interface, uses this interface to manipulate the container through an iterator. Because the interface is unchanged, the implemented container can be changed arbitrarily. This facilitates programming, debugging, and scaling. Perhaps one day, we produce software can also want to DIY a PC as simple, as long as the corresponding implementation module, through simple assembly and debugging can create a software. This is such an exciting thing ^_^. However, there may be a lot of programmers out of work by then. Oh, after all, writing a class library does not require a lot of people.

Although the STL is not object-oriented, I think everyone will be excited and overwhelmed by its creativity and high performance.

First, the container

As the most important component of the STL-container, divided into vectors (vector), two-terminal queue (deque), table (list), queues (queue), stack (stack), set (set), multiple sets (multiset), mapping (map), Multiple mappings ( Multimap).

Container Characteristics The header file
Vector vectors Can use constant time to access and modify arbitrary elements, in the sequence of the tail to insert and delete, with constant time complexity, the insertion and deletion of any item of time complexity and to the end of the distance in direct proportion, especially to the vector head to add and delete the cost is surprisingly high <vector>
Two-terminal queue deque Basically the same as the vector, the only difference is that its insert and delete operations in the sequence header also have constant time complexity <deque>
Table List Access to any element is proportional to the distance to both ends, but the cost of inserting and deleting an item at a location is constant. <list>
Queues Queue Inserts can only be done at the tail, delete, retrieve, and modify only allowed from the head. According to the principle of advanced first out. <queue>
Stacks stack A stack is a finite sequence of items, and the item that is deleted, retrieved, and modified in a sequence can only be the most recently inserted sequence. In terms of the LIFO principle <stack>
Set Set A red-black tree of nodes, each containing an element that is arranged between nodes in a predicate that acts on the pairs of elements, and no two different elements can have the same order, with the ability to find quickly. But it's at the expense of the efficiency of the insert-car removal operation. <set>
Multiple set Multiset is essentially the same as a collection, but can support the ability to find quick lookups with repeating elements <set>
Mapping map A set of {key, value} pairs that are arranged by a function Yu Yu the predicate on. With Quick lookup capability <map>
Multiple set Multimap A key can correspond to many values than a mapping. With Quick lookup capability <map>

Considering the different actual needs, more important is the need for efficiency, we can choose different containers to implement our program, so as to achieve our goal of high performance. This is also a good use of STL a difficult point, but this is also the key.

Second, the algorithm

The algorithm is mainly composed of the main header files <algorithm>,<numeric> and <functional>. <algorithm> is the largest of all STL header files, it is composed of a large stack of template functions, it can be considered that each function is to a large extent independent, which is commonly used in the scope of the function of comparison, Exchange, find, traverse operations, copy, modify, remove, reverse, sort, Mergers and so on. The <numeric> is small in size and includes only a few template functions for simple mathematical operations on the sequence, including additions and multiplication on the sequence. In <functional>, some template classes are defined to declare function objects.

STL algorithm is also very good, most of them are generic, basically use the C + + template to achieve, so that many similar functions do not have to write their own, as long as the function template on the OK.

When we use the algorithm, for different containers, such as: The Search for the collection, it is best not to use the common function find (), it is used in the collection, the performance is very poor, it is best to use the collection of the Found () function, which is optimized for the set, performance is very high.

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.