Overview of C ++ STL

Source: Internet
Author: User
Tags bit set
Introduction

C ++ STL can be divided into standard containers,AlgorithmAnd function objects, iterators and distributors, using the C ++ StandardProgramLibrary, which can greatly reduce ourCodeTo improve code stability and robustness.

Standard container

C ++ standard containers are divided into sequence containers and associated containers. For sequence containers, C ++ provides the following basic sequences:

Vector supports random access and is not suitable for scenarios with frequent insert and delete operations.

List two-way linked list, suitable for element insertion and deletion, not random access

Deque is also a double-ended sequence, but after optimization, its double-ended operation efficiency is similar to list, and the access efficiency is close to vector.

Starting from them, by defining appropriate excuses

Stack is implemented using deque by default.

The default queue is deque.

Priority_queue is a vector storage element by default, and the most likely implementation is heap.

For associated containers, C ++ provides the following:

Map ing

Mulitimap multi- ing. Compared with map, duplicate keys are allowed.

Set is regarded as a map, and its values are irrelevant.

Mulitiset allows duplicate keys compared to set.

Bitset bit set

Hash_map hash ing: implements a hash function to implement containers as a hash list to reduce the time required for searching elements.

 

Standard container specific usage can refer to the C ++ online manual STL container: http://www.cplusplus.com/reference/stl/

 

Algorithm and function object

The container itself is useful because it provides some basic operations, such as determining the size, iteration, replication, sorting, and searching. The standard library provides many algorithms to serve the most common and basic needs of container users.

There are only 60 standard library algorithms. Each algorithm is described as a template function or a set of template functions. For example, the most common sorting algorithm is sort, which can be sorted with good average efficiency, we recommend that you use sort to replace the C-language qsort for sorting. Sort can better combine containers.

For more information about the standard library algorithms and function objects, see the C ++ programming language algorithms and function objects developed by C ++, this chapter teaches you how to use standard library algorithms and function objects.

The algorithm Declaration of the standard library is in <algorithm>, and the function object is in <functional>.

 

Standard Library algorithm specific usage reference C ++ online manual STL algorithm: http://www.cplusplus.com/reference/algorithm/

 

Iterator and distributor

The iterator is the link connecting containers and algorithms, so that the algorithm writer does not have to worry about the specific details of various data structures. The Allocator provides a ing, map low-level data models in bytes to advanced object models.

Iterator is one of the concepts that every programmer needs to care about, but it is only a support mechanism. The standard library already provides the default distributor, and few programmers need to write new one.

The iterator will understand it when it comes to the STL container. Generally, the iterator can be divided into forward iterator, reverse iterator, insert iterator, and check iterator.

The standard library provides a standard distributor for allocating and releasing storage. The standard Allocator template in <memory> allocates storage with operator new, all standard containers use it by default. Of course, you can implement a distributor by yourself. The implementation of the container requires the allocate () or deallocate () object again and again, it means a large number of new calls. You can use a storage pool with a fixed size storage block, which is more efficient than the common operator new.

 

Summary

After learning the C ++ standard library, you can first learn the simple usage of standard containers, such as vector and map, and then get familiar with the algorithm. It is also a simple usage of several functions of the learning algorithm, such as sort and swap.

Learn to apply and learn theories: Consider reading the standard library of the C ++ programming language developed by the father of C ++. You can also read a thicker book: C ++ standard library.

Finally, you need to read the C ++ container and algorithm manuals in your work and programming. For more information, see the preceding URL:

C ++ online manual STL containers: http://www.cplusplus.com/reference/stl/

C ++ online manual STL algorithm: http://www.cplusplus.com/reference/algorithm/

This section details every function of each container and algorithm. There are almost 95% examples, which is a good reference.

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.