Iterator)
Iterator:
Iterators are pointer-like objects. STL algorithms use them to traverse object sequences stored in containers.
Five types: 1. Input iterator
2. Output iterator
3. Forward iterator
4. Bidirectional iterator
5. Random Access iterator
Common iterators:
Istream_iterator <> input stream iterator
Istreambuf_iterator <> input stream block iterator
Ostream_iterator <> output stream iterator
Ostreambuf_iterator <> output stream block iterator
Back_insert_iterator <Container> use the push_back member function of Container.
Front_insert_iterator <Container> use the push_front member function of the Container.
Insert_iterator <Container> use the insert MEMBER function of Container
Reverse_iterator <Container> use the Container's insert member function from the back and forth.
Const -- iterator <>
Allocators)
Check the default allocator in stl:
Namespace std {
Template <class T>
Class allocator {
Public:
// Type definitions
Typedef size_t size_type; // represent the size of the largest object in the allocation model
Typedef ptrdiff_t difference_type; // The type for signed integral values that can represent the distance between any two pointers in
// Allocation model
Typedef T * pointer;
Typedef const T * const_pointer;
Typedef T & reference;
Typedef const T & const_reference;
Typedef T value_type; // The type of the elements
// Rebind allocator to type U
Template <class U>
Struct rebind {
Typedef allocator <U> other;
};
// Return address of values
Pointer address (reference value) const;
Const_pointer address (const_reference value) const;
// Constructors and destructor
Allocator () throw ();
Allocator (const allocator &) throw ();
Template <class U>
Allocator (const allocator <U> &) throw ();
~ Allocator () throw ();
// Return maximum number of elements that can be allocated
Size_type max_size () const throw ();
// Allocate but don't initialize num elements of type t
Pointer allocate (size_type num,
Allocator <void >:: const_pointer hint = 0 );
// Initialize elements of allocated storage p with value
Void construct (pointer p, const T & value );
// Delete elements of initialized storage p
Void destroy (pointer p );
// Deallocate storage p of deleted elements
Void deallocate (pointer p, size_type num );
};
}
After reading the allocator above, we have basically known its usefulness. It is generally used in containers and serves as a member of the container, but is generally passed in using template parameters, in this way, we can change to our custom allocator.
Introduction to three containers
Introduction to STL standard containers
Standard containers
Ordered container
Vector is equivalent to an array. It can be quickly inserted and deleted from the back to directly access any element.
Deque dual queue, which can be quickly inserted and deleted from the front or back to directly access any element
List double-stranded tables, fast insertion and deletion from anywhere
Associated container www.2cto.com
Set quick search, repeated values not allowed
Multiset quick search, allowing repeated values
Map one-to-one ing, fast search based on keywords, repeated values are not allowed
Multimap one-to-multiple ing, fast search based on keywords, allowing repeated values
Container Adapter
Stack first-in-first-out
Queue first-in-first-out
Priority_queue the highest priority element is always the first column
Common functions in all standard Libraries
The default constructor provides the constructor initialized by default by the container.
The copy constructor initializes the container as the constructor of the existing similar container copy.
Memory-organized destructor no longer needed for containers
True is returned if the empty container has no elements; otherwise, false is returned.
Max_size returns the maximum number of elements in the container.
Size returns the current number of elements in the container.
Operator = assign one container to another
Operator <if the first container is smaller than the second container, true is returned; otherwise, false is returned,
Operator <= if the first container is less than or equal to the second container, true is returned; otherwise, false is returned.
Operator> If the first container is greater than the second container, true is returned; otherwise, false is returned.
Operator> = if the first container is greater than or equal to the second container, true is returned; otherwise, false is returned.
Operator = If the first container is equal to the second container, true is returned; otherwise, false is returned.
Operator! = If the first container is not equal to the second container, true is returned; otherwise, false is returned.
Swap two container elements
Operator>, operator >=, operator <, operator <=, operator =, operator! = Not applicable to priority_queue
Sequential container and associated container common functions
Begin this function two versions return iterator or const_iterator, reference the first element of the container
End this function returns two versions of iterator or const_iterator, referencing the last element of the container
Rbegin the two versions of the function return reverse_iterator or const_reverse_iterator, referencing the last element of the container
Rend. The two versions of this function return reverse_iterator or const_reverse_iterator, which references the first element of the container.
Erase clears one or more elements from the container.
Clear all elements in the container
The following table shows the typedef commonly used in sequential containers and associated containers. These typedef are commonly used in the general declaration of variables, parameters, and function return values.
Type of elements stored in the value_type container
Reference container stores reference of element type
The const_reference container stores constant references of the element type. This reference can only read elements in the container and perform the const operation.
Pointer for storing element types in the pointer container
Iterator points to the iterator that stores element types in the container
Const_iterator points to the constant iterator that stores element types in the container and can only read elements in the container.
The reverse_iterator points to the reverse iterator that stores element types in the container, which reversely iterates in the container.
Const_reverse_iterator directs to the reverse iterator that stores element types in the container and can only read elements in the container.
Difference_type refers to the type of the result of two iterators subtract from the same container (list and associated containers do not define operator -)
Size_type is used to calculate the number of items in the container and the type of the retrieval sequence container (list retrieval is not allowed)
Comparison of four containers
Vector (continuous space storage, you can use the [] Operator) to quickly access random elements, quickly Insert elements at the end, but insert Between the ages of the sequence, it is slow to delete elements, and if the space allocated at the beginning is not enough, there will be a performance overhead for re-allocating larger space and copying.
Deque) quick access to random elements, fast insertion of elements at the beginning and end, random insertion, slow deletion of elements, and faster Space reallocation than vector, the original elements do not need to be copied. To sort a deque, copy the deque to the vector first, and then copy it back to deque.
List (each element is connected by a linked list) accessing random elements is not as fast as vector, and random inserted elements are faster than vector. Therefore, space is allocated to each element. Therefore, there is no sufficient space and the allocation is re-allocated.
The internal elements of the set are unique and stored in a balance tree structure. Therefore, the elements are sorted during traversal and the search speed is relatively high.
The key of a map must be unique.
Multiset
Multimap
The stack adapter must be used with other containers. The default internal container in stl is deque. First, only one exit is available, and traversal is not allowed.
Queue is a restricted deque. It is easier to use a list for internal containers. First-in-first-out, traversal not allowed.
Vector <bool> and bitset <>. The length can be changed dynamically.
The elements inserted by priority_queue have the highest priority.
Valarray is specialized in numerical calculation and adds special mathematical functions.
From the column qin3232521