STL container, algorithm, iterator total

Source: Internet
Author: User
I. Container
As the main component of STL, containers are divided into vector, deque, list, queue, and stack ), set, multi-set, map, and multimap ).

Container
Features
Header file

Vector
You can use constant time to access and modify any element. It has a constant time complexity when inserting or deleting the end of the sequence, the time complexity of inserting and deleting any item is proportional to the distance to the end, especially the cost of adding and deleting vector headers is astonishing.
<Vector>

Deque
Basically the same as a vector, the only difference is that the insertion and deletion operations on the sequence header also have constant time complexity.
<Deque>

Table list
Access to any element is proportional to the distance between the two ends, but it takes a constant time to insert or delete an item at a certain position.
<List>

Queue
Insert can only be performed at the end, and delete, search, and modify can only be performed from the header. Follow the principle of first-in-first-out.
<Queue>

Stack
A stack is a finite sequence of items. Items that are deleted, retrieved, and modified in a sequence can only be items that are recently inserted. That is, according to the principle of "back-to-first-out"
<Stack>

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, quick search is available. However, it is at the cost of the efficiency of the inserted car deletion operation.
<Set>

Multi-set Multiset
It is basically the same as a set, but it can support repeated elements for fast search
<Set>

Map
A set of {key, value} pairs that are arranged in a predicate that acts on the key pair. Quick Search
<Map>

Multimap
Compared with ing, a key can correspond to more values. Quick Search
<Map>

Considering different actual needs, and more importantly, the efficiency needs, we can select different containers to implement our programs to improve our performance. This is also a difficult point to use STL, but it is also the key.

Ii. Algorithms
The algorithm consists of the header file <algorithm>, <numeric>, and <functional>. <Algorithm> is the largest of all STL header files. It consists 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> it 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.

STL algorithms are also excellent. Most of them are generic and are basically implemented using C ++ templates. In this way, many similar functions do not need to be written by themselves, you only need to use the function template.

When using algorithms, we need to target different containers, such as searching for sets. It is best not to use the common function find (). When using the set, the performance is very poor, it is best to use the find () function provided by the set, which is optimized for the set and has high performance.

Iii. iterator
In <itertator>, we can fully implement it regardless of how the iterator class is implemented. Most of the time, it is okay to understand it as a pointer (a pointer is a special case of the iterator, and it also belongs to the iterator). However, it cannot be done completely.

Iterator functions (abilities of iterator gategories)

Input iterator

Input iterator
Forward read

Reads forward
Istream

Output iterator

Output iterator
Write forward

Writes forward
Ostream, inserter

Forward iterator

Forward iterator
Forward read/write

Read and writes forward

Bidirectional iterator

Bidirectional iterator
Forward and backward read/write

Read and writes forward and

Backward
List, set, Multiset, MAP, MUL

Timap

Random iterator

Random Access iterator
Random read/write

Read and Write with random

Access
Vector, deque, array, string

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.