C ++ Primer study note _ 45_STL practice and Analysis (19) -- Structure of generic algorithms

Source: Internet
Author: User

STL practice and analysis-structure of generic algorithms



Introduction:

Just as all containers are built on consistent design patterns, algorithms share a common design basis.

The most basic attribute of an algorithm is the type of iterator to be used. All algorithms specify the types of iterators that can be used by each iterator parameter. For example, if the parameter must be a random access iterator, you can provide a vector or deque type iterator or a pointer to an array. The iterators of other containers cannot be used in such algorithms.

C ++ also provides two algorithm modes:A pattern is defined by the algorithm's parameters.;Another mode is defined by two function names and the specification of the overload..


I. algorithm parameters

Most algorithms use the following four parameter modes:

alg(beg,end,other parms);alg(beg,end,dest,other parms);alg(beg,end,beg2,other parms);alg(beg,end,beg2,end2,other parms);

The alg is the name of the algorithm, and beg and end specify the element range of the algorithm operation. This range is usually the "input range" of the algorithm ".


1. Algorithms with a single target iterator

The dest parameter is an iterator used to specify the target object for storing output data.

When calling these algorithms,Make sure that the output container has enough capacity to store output data.

If dest is an iterator on a container, the algorithm writes the output content to an existing element in the container. More commonly, dest is bound to an insert iterator or ostream_iterator.Insert iterator to add elements to the container,To ensure that the container has enough space for storage and output.Ostream_iteratorThe write output stream function is implemented.,No number of elements to be written.


2. algorithm with the second input sequence

Some algorithms contain a 2 iterator parameter or a beg2 and end2 iterator parameter to specify the second input range. At this time, the algorithm has two complete ranges: beg and end mark the first input range, while beg2 and end2 mark the second input range.

Like the algorithm written into dest, the algorithm with only the beg2 operator assumes that the sequence starting with a beg2 is as large as the sequence marked by beg and end!


Ii. algorithm naming rules

The standard library uses two important algorithm naming and heavy load specifications:

1) The first mode includes an algorithm for testing the elements in the input range.

2) The second mode is applied to the algorithm for sorting elements in the input range [not very understandable]


1. distinguish an algorithm version with a value or a predicate function parameter

Many algorithms implement their functions by checking the elements in the input range. Most of the algorithms provide functions of the second version, allowing programmers to use comparison or test functions instead of operators (<OR =.

Sort (beg, end); // carries an additional form parameter, indicating different operations for element sorting sort (beg, end, comp );

The = Operator is used by default to check the algorithm of the specified value. The system provides additional named (rather than overloaded) versions for these algorithms:

    find(beg,end,val);    find_if(beg,end,pred);

The name of an algorithm with a predicate function parameter has a _ if suffix. The find_if algorithm is used to find an element that causes the pred function to return a non-zero value.


2. 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,Elements that will be rearranged by these algorithmsWrite back the input range. 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);    reverse_copy(beg,end,dest);

// P360 exercise 11.27 // describes the functions of these algorithms based only on the names and parameters of these functions. Replace (beg, end, old_val, new_val); replace_if (beg, end, pred, new_val); replace_copy (beg, end, dest, old_val, new_val); replace_copy_if (beg, end, dest, pred, new_val );

// Exercise 11.28int main () {list <int> iList; for (list <int>: size_type I = 0; I! = 100; ++ I) {iList. push_back (I);} vector <int> vec; reverse_copy (iList. begin (), iList. end (), back_inserter (vec); for (vector <int >:: iterator iter = vec. begin (); iter! = Vec. end (); ++ iter) {cout <* iter <endl ;}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.