The first blog post, Hope is not the last article. O (∩_∩) o haha
The next few posts will introduce the STL's super-usable sequential containers from shallow to deep.
- Container: is a collection of certain types of objects.
- Sequential containers: definition: Sequential containers are a class of containers. In this class container, the position of the object (element) in the container corresponds to the order in which the container is joined, and does not depend on the value of the element.
Note: Sequential containers can hold almost any type of element, but may restrict element container operations. Please see the next blog post for details.
- All sequential containers are common in nature: so sequential containers provide the ability to quickly sequentially access elements.
Types of sequential containers
Vector (Common) |
1. Variable size one-way tail array 2. Support Fast random access 3. Do not insert delete later may be slow |
Deque |
1. Variable size bidirectional queue 2. support for fast random access 3. Insert Delete in the kinsoku soon |
List |
1. Variable size bidirectional linked list 2. only sequential access is supported 3. Insert Delete at any location quickly |
Forward_list (C++11) |
1. Variable size unidirectional head chain List 2. only sequential access is supported 3. Insert Delete at any location quickly |
Array (c++11) |
1. Fixed size array 2. supports fast random access 3. No insert Delete but can be assigned value
|
String |
1. Like vector, special-purpose, specially used to such as: ' A ', ' B '
|
Description: Order: 1. Elements are stored in contiguous memory space and are searched quickly with subscript.
But not in the open place (opening: vector/string at the tail, deque at the end) Insert Delete (array side to play) will be very slow.
2. When accessed with iterators, arithmetic operators are supported. Such as:
1 std::vector<int>::iterator it = v.begin (); 2 it = it +2;
random : 1. To access an element, you need to access the entire container. But insert Delete (array you come again) soon.
2. When accessed with iterators, arithmetic operators are not supported, but self-increment is supported. Such as:
1 std::list<int>::iterator it = v.begin (); 2 it = it +2// error 3 it++; 4 it++;
- About C++11 Standard containers:
Array: Design purpose: More secure, easier to use array type.
However, the size is fixed (security), so any change in container size is not supported.
Forward_list: Designed to achieve performance comparable to the performance of the best unidirectional list we write.
Special: No size operation. For other containers, the size operation guarantees a constant-time operation (including list)
- Select the principle of the sequential container:
1. Vertor is the best choice, unless you have a better choice.
2. There are a lot of small elements, space overhead is important. Don't use lists and forward_list.
3. To randomly access: select vectors and Deque
4. Require intermediate Insert Delete: List and forward_list
5. Requirements for wonderful, such as: to be inserted in the middle, and then to random access.
Can be solved in a variety of ways: a. List to save and copy to vector.
B. Insert directly with the tail of the vector, then sort the middle with the STL
C.....
Tips: For common operations (such as the operation of a template), use iterators, without subscript, to avoid the possibility of random access.
2015-04-15
C++11 Sequence Container Summary (1)