STL1 -- sequence container and sequence container adapter, stl1 Adapter
Container
A container is a collection of specific types of objects. The types of containers are divided into ordered containers, container adapters, and associated containers. Ordered containers aggregate a single type of elements into containers, then, these elements are stored and accessed based on their locations.
The order of elements in an ordered container is independent of the element value. It is determined by the order in which the elements are added to the container. The standard library defines three sequence container types: vector, list, And deque. The difference lies in the method of accessing elements and the running cost of adding or deleting element-related operations.
Based on the operations provided by the original container type, the adapter can adapt to the basic container type by defining a new operation interface. The ordered container adapters include the stack, queue, and priority_queue types.
The standard library also defines several associated containers whose elements are sorted by keys.
The standard library imposes a public interface on operations defined by the container type. The difference between these container types is what operations they provide, but if the two containers provide the same operations, their interfaces (function names and number of parameters) are the same. The operation set levels of the container type are as follows:
1. Some operations apply to all container types;
2. Some operations are only applicable to a subset of the sequence or associated container type;
3. Some operations are only applicable to a subset of the sequence or associated container type;
Initialization and definition of ordered containers
To use an ordered container, you must first include the header file:
#include<vector>#include<list>#include<deque>
Definition of ordered containers
Vector <int> vec; // all containers are the type of elements in the template list <int> lis; // <>, it can be a common data type or a custom class type </span> deque <int> items; // The container type defines the default constructor used to create an empty container object of the specified type. The default constructor does not contain parameters.
Sequential container Initialization
Vector <int> vec; // defines the container vector <int> vec1 (10) that contains int elements ); // container vector <int> vec2 () that contains 10 int elements; // each container containing 10 int elements is initialized as 1, only applicable to the ordered container vector <int> vec3 (vec2); // copy the elements in vec2 to vec3, only applicable to the ordered container
Elements in one container cannot be directly assigned to another container, but this function can be indirectly implemented by passing a pair of iterators.
Vector <string> svec; list <string> ilist (svec); // different containers, cannot copy list <string> slist (svec. begin (), svec. end (); // use the iterator, which does not require the same container type
Note: Except the reference type, all built-in or composite types (including the container type itself) can be used as element types, and the IO library type cannot be used as the container type;
If the class type is used as the element type and the container size is specified, a single initialization constructor must be provided.
vector< <vector<string> >lines;
Sequential container operations
Type alias of the ordered container
Begin and end members
All containers provide the begin and end operators to return the iterator. The begin operator returns the first element pointed to by the iterator, and the end operator returns the next element of the container end pointed to by the iterator, it points to a non-existent element. when the container is empty, the values returned by the two operators are equal.
Adding elements to a container
Container supports the following operations to add new elements:
Any insert or push operations may invalidate the iterator. When writing a program, the program must ensure that the iterator is updated after each insert operation. To avoid the iterator returned by the end operation.
Relational operators
All containers support Relational operators to compare elements in containers. During comparison, the types of elements in containers and containers must be the same. Container comparison is based on the comparison of elements in the container, that is, the comparison operation defined by the internal element type. If the element type of the container does not support certain operators, the container cannot perform comparative operations.
Container size operation
Access Element
Delete Element
Assignment and swap
When the value assignment operator is used, the length of the first two containers can be different, but both containers have the length of the right operand.
= And assign will invalidate all iterators of the left operand container, and swap will not.
Assign can assign values to elements of different types But compatible with each other.
Swap implements the function of exchanging all elements in two containers. The types of containers to be exchanged must match, the operands must be containers of the same type, and the stored element types must also be the same.
Capacity and reserve
Because vector elements are stored in a continuous manner, when new elements are added, but the container does not have the space to accommodate the new elements, the memory space will be re-allocated and the elements stored in the space will be copied, then insert new elements and cancel the old bucket. To enable fast memory allocation for a vector, the actual allocated capacity is much larger than the current space required.
Vector provides two member functions capacity and reserve to interact with the container's memory allocation.
The capacity operation obtains the total number of elements that can be stored before the container needs to allocate more storage space, which is usually larger than the size.
The reserve Operation tells the container how many elements should be reserved for storage space.
Container Adapter
An adapter is a mechanism that makes the behavior of one thing look similar to the behavior of another thing. An adapter enables an existing container type to work in another different abstract type. For example, stack can implement any sort of ordered container in stack mode.
The adapter must contain header files.
#include<stack>#include<queue>
Common Operations and types of container adapters
Adapter Initialization
All adapters have two constructors. 1. default constructor: Common empty objects. 2. constructor with one container parameter: Replace the parameter container as the basic value.
deque<int>deq;stack<int>stk(deq);
Override basic container types
The default stack and queue are implemented based on the deque container, while priority_queue is implemented on the vector container.
To overwrite the associated container type, you can specify an ordered container as the second type parameter of the adapter:
stack< string,vector<string> >str_stk;
For a given adapter, the associated container must meet certain constraints.
Stack can be associated with all sequential containers;
Queue requires that the associated containers support push_front, so they can only be created on list and deque;
Priority_queue requires the random access function, so it is only built on vector and deque;
Stack Adapter
Stack operations are as follows:
All container adapters define their own operations based on the operations supported by the Basic container type. For example, stack is built on the deque container, so the operation provided by deque is used to implement the stack function.
Queue and priority queue
Priority_queue allows users to set priority for the elements stored in the queue. This queue does not directly place the new elements at the end of the queue, but is placed before the elements with lower priority than the queue.
Iterator and iterator operations
An iterator is a data type that checks elements inside a container and traverses elements. The standard library defines an iterator type for each standard container, such as a vector iterator:
vector<int>::iterator iter
All iterators have the same interface. For example, the iterator can use the unreferenced operator (* operator) to access the elements pointed to by the iterator.
* Iter = 0 // set iter to 0.
The following table lists the operations provided by the iterator for all standard library container types.
Deque and vector also provide additional Operators
The vector and deque containers provide fast random access to their elements, while the list only supports auto-increment, auto-subtraction, and Equality operations.
Const_iterator
This type can only be used to read elements in the container but cannot change its value.
Comparison of various containers
Vector: Provides Random Access to elements. Except the tail of the container, the insert (or delete) operation at any location of the container requires moving all elements on the right of the inserted (or deleted) element.
List: Indicates discontinuous memory areas. An element can be efficiently inserted or deleted anywhere, but random access is not supported.
Deque: It is very fast to insert and delete elements at both ends of the deque queue, and deque has the following properties: 1. insert or delete objects in the container is inefficient. 2. insert and delete operations in the header efficiently, just like at the end of the container. 3. supports random access to all elements; 4. inserting an element at the beginning or end of the container does not invalidate any iterator. Deleting the element at the beginning or end will make the iterator pointing to the deleted element effective, insert and delete operations at any other location will invalidate all iterators pointing to the elements of the container.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.