STL (Standard Template Library) Standard Template Library
I. STL has two features:
1. It separates algorithms from data structures.
2. It uses the template in C ++.
Both features aim to make it more universal. For example, the sort function in STL can be used to operate data structures such as arrays and linked lists.
2. STL has three basic components:
1. iterator: provides methods to access container objects.
2. Container: a data structure, such as list and vector.
3. algorithm: A template function used to operate data in a container.
Iii. header files
To avoid conflicts with other header files, STL no longer uses. h header files.
For example, the following example uses the string class, iterator, and algorithm:
# Include <string>
# Include <iterator>
# Include <algorithm>
The following are the containers contained in some header files:
<Deque> deque
<List> List
<Queue> queue priority_queue
<Map> map multimap
<Set> set Multiset
<Stack> Stack
<Vector> vector <bool>
Iv. iterator details
STL provides five iterators:
1. Input iterators provides read-only access to data.
2. Output iterators provides write-only access to data.
3. Forward iterators provides read/write access and an iterator that can be pushed forward.
4. bidirectional iterators provides read/write access and can perform forward and backward operations.
5. Random access iterators provides read/write access and can be moved randomly in data.
The pointer is also an iterator. To illustrate this example, we use the pointer as an iterator for searching common Arrays Using the find () algorithm in STL.
# Include <iostream. h>
# Include <algorithm>
Using namespace STD;
# Define size 100
Int iarry [size];
Int main ()
{
Arry [20] = 50;
Int * P = find (iarry, iarry + size, 50 );
If (IP = iarry + size)
Cout <"50 not found" <Endl;
Else
Cout <"50" found <Endl;
Return 0;
}
For comparison, here we use a search example of container iterator. Unlike pointers, we can use the container class to extract iterator objects. Two typical iterator methods, begin () and end (), represent the container range in most containers.
The following uses vector instead of array:
# Include <iostream. h>
# Include <algorithm>
# Include <vector>
Using namespace STD;
Vector <int> intvectors (100 );
Int main ()
{
Intvector [20] = 50;
Vector <int>: iterator intlter = find (intvector. Begin (), intvector. End (), 50 );
If (intlter! = Intvector. End ())
Cout <"vector container inclusion" <* intlter <Endl;
Else
Cout <"the vector container does not contain 50" <Endl;
Return 0;
}
5. Containers (vector encapsulation array, list encapsulation chain table, MAP and set encapsulation Binary Tree, etc)
Vector-a standard and secure array in STL. It is equivalent to an array. It can be quickly inserted and deleted from the back to directly access any element.
Deque (double-ended Queue) -- similar to vector in function, but data can be added to both ends.
List-the cursor can only move one step at a time. If you are familiar with the linked list, the list in STL is a two-way linked list (each node has two pointers pointing to the front and back ).
Combined container
(Set Multiset map multimap is called an associated container. They all map a key value to an element and use this key to search, insert, delete, and perform other operations, they achieved a better logarithm time effect than the linear time for the insert/delete operation. When the number of elements increases, they use binary search internally, which is more efficient; however, if we don't need this extra efficiency, we can avoid the considerable complexity and Extra pointer space caused by good time ;)
Set-elements in the container are automatically sorted by size. The key and value of an element are combined into one, and the values of these data must be unique.
Multiset (Multi-replica set) -- similar to set, however, the values do not need to be unique (that is, they can be repeated ).
Map (ing)-A sorted set of binary groups. Each element in map is composed of two values. The key (key value, the key value in a map must be unique.) It is used for sorting or search. Its value can be retrieved again in the container. The other value is the value associated with the element. For example, in addition to finding a data in Ar [43] = "overripe", map can also find a data in Ar ["banana"] = "overripe. If you want to obtain the element information, you can simply enter the full name of the element.
Multimap (Multi- ing) -- similar to ing (MAP), however, key values do not need to be unique (that is, they can be repeated ).
Container Adapter
Stack first-in-first-out
Queue first-in-first-out
Priority_queue the highest priority element is always the first column
Vi. Algorithms
1. Sorting Algorithm
There are several sorting algorithms in STL:
Sort |
Sorts all elements in a given range. |
Stable_sort |
Performs a stable sorting of all elements in a given range. |
Partial_sort |
Sorts all elements in a given range. |
Partial_sort_copy |
Copy and sort a given interval |
Nth_element |
Find the elements corresponding to a location in a given range |
Is_sorted |
Determine whether the order of an interval has been sorted |
Partition |
Place elements that meet certain conditions in front of them. |
Stable_partition |
Relatively stable putting elements that meet certain conditions in front |
By default, STL sorts data from small to large. If you do not want to sort by default,
Then you can use the following functions provided by STL:
Others will continue to be written when they are free.
Performance_to |
Equal |
Not_0000_to |
Not equal |
Less |
Less |
Greater |
Greater |
Less_equal |
Less than or equal |
Greater_equal |
Greater than or equal |
Vector <int> vect;
Sort (vect. Begin (), vect. End ());
// Call
Sort (vect. Begin (), vect. End (), less <int> ());
You can also define a function as follows:
Bool function name (const parameter type & Parameter 1, const parameter type & Parameter 2)
{
Return Boolean value;
}