The standard library does not define member functions that implement these operations for each container type, but defines a groupGeneric Algorithms: Because they implement common operations, they are called "algorithms "; "Generic" means that they can operate on multiple container types-not only can they operate on vector or list standard library types, it can also be used in a sequence of built-in array types, or even other types.
1. The standard algorithm is inherently independent of the type and container type. It only implicitly depends on the element type at one point: it must be able to compare the elements. The algorithm has the following clear requirements:
* You need a way to traverse the set: you can move the element forward to the next element.
* You must be able to know whether it has reached the end of the set.
* You must be able to compare each element in the container with the element to be searched.
* A type is required to indicate the position of an element in the container, or the element cannot be found.
The iterator binds the algorithm to the container. The generic algorithm uses the iterator to solve the first requirement: traverse the container. All iterators support the auto-increment operator, which positions the next element from one element and provides the unreferenced operator to access the value of the element. In most cases, each algorithm requires at least two iterators to indicate the element range manipulated by the algorithm. The first iterator points to the first element, while the second iterator points to the next position of the last element. The elements pointed to by the second iterator [sometimes referred to as the excess end iterator] are not the elements to be operated, but are used as the Sentinel to terminate traversal.
These algorithms never use container operations, so their implementation is irrelevant to the type. All access and traversal of elements are implemented through the iterator. The actual location of the container type (even whether the processed elements are stored in the container is unknown ). Generic algorithms do not execute container operations, but rely solely on the iterator and iterator operations. They are implemented based on the iterator and its operations, rather than container operations.
2. The generic algorithm must contain the algorithm header file. The standard library also defines a set of generalized arithmetic algorithms, which are used to the same generic algorithms. These algorithms must contain the numeric header file.
Except for a few exceptions, all algorithms operate on elements within a range. This range is called "input range ". Algorithms with input range parameters always use the first two parameters to mark the range.These two parameters are the iterator pointing to the next position of the first element to be processed and the last element respectively.
Read-Only Algorithm
Accumulate has three parameters. The first two Parameters specify the range of elements to be accumulated. The third parameter is the initial value of accumulation. The return type of the accumulated result returned by the algorithm is the type of the third real parameter. It is necessary to specify the third real parameter for accumulating the starting value, because accumulate has no idea about the element type to be accumulated.
The find_first_of algorithm has two sets of iterator parameters to mark the element ranges of two segments. In the first range, find the elements matching any element in the second range, and return an iterator, point to the first matched element. If no matching element is found, the end iterator In the first range is returned. In each pair of iterators, the types of two real parameters must be exactly matched, but the type matching between the two teams is not required. In particular, elements can be stored in different types of sequences, as long as the elements of these two sequences can be compared.
Algorithm for writing container elements
Fill has a pair of iterators for specifying the range to be written, and the written value is a copy of its third parameter. If the input range is valid, data can be written securely. This algorithm only writes existing elements in the input range.
The fill_n function includes the following parameters: An iterator, a calculator, and a value. The function assumes that writing to a specified number of elements is safe.Common error: Call the fill_n function on an empty container without elements.Write operations on the specified number of elements or the algorithms written to the target iterator do not check whether the target size is sufficient to store the elements to be written.
Back_inserter one way to ensure that the algorithm has enough elements to store output data is to useInsert iterator. The program that uses back_inserte must contain the iterator header file. The back_inserter function is an iterator adapter. Like the container adapter, The iterator adapter uses an object as the real parameter and generates a new object that adapts to the real parameter behavior. Fill_n (back_inserter (VEC), 10, 0); in this example, the arguments passed to back -- inserter are references of a container, generate an insert iterator bound to the container. When an iterator is used to assign values to an element, the value assignment operation calls push_back to add an element with a specified value to the container.
Copy has three iterator parameters: the first two specify the input range, and a single field points to an element of the target sequence. The target sequence passed to copy must be at least as large as the input range. Copy (ilst. Begin (), ilst. End (), back_inserter (ivec ));
Algorithm _ copy version
Replace (ilst. Begin (), ilst. End (), 0, 42) This call replaces all instances with 0 values with 42.
Replace_copy (ilst. begin (), ilst. end (), back_inserter (ivec), 0, 42) after calling the function, ilst does not change. ivec stores a copy of ilst, in ilst, all the 0 values are changed to 42 in ivec.
3. algorithm p343 for sorting container elements
Remove duplicatesThe unique function does not delete any elements, but copies non-repeating elements to the front end of the sequence to overwrite adjacent repeating elements. The iterator returned by unique points to the next position beyond the end of the element range without duplicates.
PredicateIs a function that performs some checks. It returns the type used for condition judgment and determines whether the condition is true.
4. Let's talk about the iterator.
Insert iterator: This type of iterator is bound with the container to enable element insertion in the container.
Iostream iterator: This type of iterator can be bound to an input or output stream for iterative traversal of the associated Io stream.
Echo iterator: This type of iterator is intended to traverse backward rather than forward. All container types define their own reverse_iterator types, which are returned by the rbegin and rend member functions.
Insert iterator
Back_inserter: creates an iterator that inserts data using push_back.
Front_inserter: Use push_front to insert data. It can be used only when the container provides the push_back operation.
Insert. In addition to the associated containers, insert also carries the second real parameter: pointing to the iterator at the starting position of the insert.
Example: List <int >:: iterator it = find (ilst. Begin (), ilst. End (), 42 );
Replace_copy (ivec, begin (), ivec. End (), inserter (ilst, it), 100, 0 );
The use of front_inserter will lead to the reverse appearance of elements in the target object, which is very important.
Iostream iterator
Istream_iterator <t> in (Stream); creates an istream_iterator object that reads T-type objects from the input stream STRM.
Isteam_iterator <t> In; istream_iterator object exceeds the end iterator
Ostream_iterator <t> in (STRM); creates an ostream_iterator object that writes a T-type object to the output stream STRM. Ostream_iterator does not provide an iterator that exceeds the end.
Ostream_iterator <t> in (STRM, delim); creates an ostream_iterator object that writes a T-type object to the output stream STRM, and uses delim as the element separator During writing. Delim is an array of characters ending with null characters. Otherwise, its behavior will be undefined.
The stream iterator only defines the most basic iterator operations: auto-increment, unreference, and assignment. In addition, you can compare whether two istream iterators are the same (or not ). The ostream iterator does not provide comparison operations. (Compare whether two istream_iterator objects are equal (or not ). The iterator must be of the same type. If both iterators are end values, they are equal. For two iterators that do not point to the end of the stream, if they are constructed using the same input stream, they are also equal)
You can create an istream_iterator object for any type of input operator (>.
The stream iterator has the following important restrictions:
* It is impossible to read data from the ostream_iterator object or to the istream_iterator object.
* Once a value is assigned to the ostream_iterator object, the write operation is committed. There is no way to change this value after the value is assigned. In addition, each different value in the ostream_iterator object can only be output once.
* Ostream_iterator does not have the-> operator.
Reverse iterator
You can define a reverse iterator from an iterator that supports both -- and ++. The stream iterator cannot reverse traverse the stream, so it cannot create a reverse iterator.
The iterators provided by many standard library containers must at least meet the requirements of two-way iterators.
Although the map and set types provide bidirectional iterators, the associated containers can only use one subset of the algorithm. The problem is that the key of the associated container is the const object. Therefore, the associated container cannot use any algorithm to write sequence elements. Only the iterator associated with the associated container can be used to provide real parameters for read operations. When processing algorithms, it is best to regard the iterator on the associated container as an input iterator that supports the auto-subtraction operation, rather than a complete bidirectional iterator.
5. Structure of generic algorithms
Most algorithms use one of the following four forms:
ALG (beg, end, other parms );
ALG (beg, end, DEST, other parms); the Dest parameter is an iterator used to specify the target object for storing output data. The algorithm assumes that no matter how many elements need to be written, it is safe. If DEST is an iterator on a container, the algorithm writes the output content to an existing element in the container. A more common usage is to bind DEST with an insert iterator or ostream_iterator. Ostream_iterator is used to write the output stream without considering the number of written elements.
ALG (beg, end, beg2, other parms );
ALG (beg, end, beg2, end2, other parms );
Algorithm naming rules
The standard library uses a group of identical naming and reload specifications, including two important modes: The first mode includes an algorithm for testing elements in the input range, the second mode applies to the algorithm for sorting elements in the input range.
A. Different algorithm versions with one value or a predicate function parameter
Many algorithms implement their functions by checking the elements in the input range. These algorithms usually use the standard relational operator = or <. Most of the algorithms provide functions of the second version, allowing programmers to use comparison or test functions to replace operators.
The <operator is used to sort the elements of the container again. The second overloaded version of these algorithms carries an additional parameter, indicating different operations used for element sorting:
Sort (beg, end); Use <operator to sort the elements
Sort (beg, end, comp); use function named comp to sort the elements
The = Operator is used by default to check the algorithm of the specified value. The system provides an additional named (rather than overloaded) version for this type of algorithm, with a predicate function parameter. An algorithm with a predicate function parameter. Its name is suffixed with _ if:
Find (beg, end, Val );
Find_if (beg, end, Pred); find first instance for which Pred is true
Standard Library wie algorithms provide zero-bit named versions instead of heavy-load versions, because these two versions of algorithms carry the same number of parameters.
B. identify whether the algorithm version for replication is implemented
Elements in the input range may be rearranged regardless of whether the algorithm checks its element values. By default, these algorithms write elements that are rearranged back to their input ranges. The standard library also provides additional named versions for these algorithms to write elements to the specified output target. This version of algorithm adds the _ copy suffix to the name:
Reverse (beg, end); sorts the elements in the input sequence in reverse order.
Reverse_copy (beg, end, DEST); copy the elements of the input sequence and store them in reverse order to the sequence starting with DeST.