Introduction to STL series (1) -- STL

Source: Internet
Author: User
Tags mscorlib

I. Introduction

Recently, I have been studying C ++ by myself. So here I will summarize my learning process during this period. In this way, I will consolidate what I learned and use it for later review. In addition, I hope this series of articles can help other friends who learn C ++ by themselves.

Since I have studied C # language before, I often compare the content in C ++ with that in C # during the process of self-learning C ++, therefore, the content of this series of articles will be compared with C # To illustrate that the language is a good idea. As long as you have a good grasp of the language, you can draw a line from other languages.

Ii. What is STL?

STL is called the Standard Template Library, which provides some common container objects and common algorithms. You can understand that STL is a Library, this library encapsulates many container classes and general methods. We can program by calling the encapsulated methods and container classes in this library. STL is like C.. NET class library. For example, in C #, the List <T> class exists in mscorlib. dll System. collections. in the Generic namespace, in C ++, list <T> exists in the std namespace in the list header file, therefore, the list <T> container should be used in the C ++ code (in C ++, the list should be a container, that is, a collection of elements ), you must first introduce the list header file through # include <list>, similar to adding mscorlib in C. dll introduction, and then use namespace std to introduce the namespace, similar to the using System in C. collec Tions. Generic Code.

Iii. Six STL components

STL abstracts common behaviors based on data structures through templates and forms a unique STL algorithm. In STL, these data structures become containers. The container and algorithm are connected through the intermediate: iterator. The iterator can be seen as a link between the data structure and algorithm, which reduces the coupling between the data structure and algorithm. STL China has six core components:

 

  • Container)
  • Algorithm (Algorithm)
  • Iterator)
  • Function object)
  • Adapter (Adaptor)
  • Space configurator (Allocator)
These six components are described below. 3.1 containers

Containers in STL can be divided into sequential containers and associated containers. The position of each element in a sequence container depends on the position set when the element is inserted, and has nothing to do with the element value, the element location of the associated container depends on the specific sorting rules, and is irrelevant to the insertion sequence. This means that the positions of each element in a sequence container correspond to the insertion sequence, and the elements in the association container sort each element according to specific sorting rules, it has nothing to do with the sequence of element insertion. For more information, refer to the articles later in this series.

Sequential containers include vector, list, And deque, while associated containers include set, multiset, map, and multimap. The following table provides a brief introduction to each container:



All the containers listed above have corresponding forms in C #. Their relations are: std: vector -- List <T> std :: list -- sorted list <T> class std: map -- SortedDictionary <TKey, TValue> class std: set -- HashSet <T> class, but it must be clear here, the set in STL uses a red/black tree as the underlying data structure, while the HashSet <T> class in C # uses a hash table as the underlying data structure, because the two use different data structures, as a result, the query efficiency is different. The set query time is O (logn), which is also the query time of the red/black tree, And the HashSet query time is O (1 ). Std: multimap -- Dictionary <TKey, List <TValue>. This class does not exist in C #. You also need to implement std: multiset -- Dictionary <TKey, int> (the second parameter stores the number of keys) std: deque in C ++ does not find the corresponding implementation in C #, but we can implement it ourselves, for specific implementation, refer to the article: A Deque Class in C #. In the preceding correspondence, the SortedDictionary <TKey, TValue> class in C # uses the binary search tree as the underlying data structure, while the Dictionary <TKey, the TValue> class uses a hash table as the underlying data structure. The table below lists the differences between the two operations because of their different data structures that lead to different operation efficiency.
3.2 AlgorithmAn algorithm is a template function used to operate data in a container. It abstracts the operation behavior on the data structure. To use the algorithm defined in STL, first introduce the header file <algorithm>. For example, the sort () function in STL can sort the data in the container, and you can use the find () function to search for an element in the container. The algorithm here can be compared with the generic method in C. 3.3 iterator

The main point of STL implementation is to separate containers and algorithms so that the two are independent of each other. The iterator connects the two. The iterator provides methods to access the container. An iterator is essentially a smart pointer, which reloads the-> and * operators. In fact, the C ++ pointer is also an iterator. In C # also has the concept of iterator, specific reference MSDN: http://msdn.microsoft.com/zh-cn/library/dscyy5s0 (v = vs.90 ). aspx, the difference is that in C ++, The iterator is divided into five types:

3.4 function objects

The function object is also called a function imitation. The function object in STL is an object that reloads the template class of the operator (), because the call method of this class object is similar to that of the function, so it is called a function object. The function object is similar to the delegate object in C #. If you are familiar with C #, you must know that we can call the delegate implicitly, that is, the delegate object (real parameter ),

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.