C + + Primer Learning notes _45_stl Practice and Analysis (19)--building routine algorithm

Source: Internet
Author: User
Tags types of functions

STLPractice and Analysis--the structure of a generic algorithm



Introduction:

just as all the containers are built in a consistent design pattern , The algorithm also has a common design basis .

The most important property of the algorithm is the type of iterator that is required to be used . All algorithms specify the type of iterator that each of its iterator shapes can use.

Suppose, for example, that a form must be a random access iterator that provides an iterator of the type vector or deque , or a pointer to an array.

The iterators of other containers cannot be used on such algorithms.

C + + two additional algorithm modes are also available : a pattern defined by the algorithm's shape ; Another pattern is defined by the specification of two types of functions named and overloaded .


First, the shape of the algorithm model

Most algorithms use the following four types of patterns:

ALG (Beg,end,other parms), ALG (Beg,end,dest,other parms), ALG (Beg,end,beg2,other parms), ALG (beg,end,beg2,end2,other Parms);

among them. ALG is the name of the algorithm. Beg and end Specify the range of elements for the algorithm operation.

We usually make this range the "input range" of the algorithm.


1 , an algorithm with a single target iterator

dest a shape is an iterator. Used to specify the target object that stores the output data.

when these algorithms are called , you must ensure that the output container has large enough capacity to store the output data.

/span> dest Is the iterator on the container The output is written to the container Span style= "color: #ff3333;" > There is a .

The more common method of use is dest with a insert iterator or ostream_iterator bound together .

Insert an iterator to add an element to a container , to ensure that the container has sufficient space to store the output. ostream_iterator The function of writing output stream, it is necessary to consider the number of elements written .


2 , an algorithm with a second input sequence

There are some algorithms with aBEG2The iterator shape participates. Or at the same time withBEG2and theEnd2An iterator shape to specify his second input range. At this point, the algorithm has a complete set of two scopes:Begand theEndmarks the first input range, andBEG2and theEnd2marks the second input range.

and Writedestthe same algorithm. Only with Beg2 The algorithm also assumes aBEG2the beginning of the sequence andBegand theEnd tag is as large as the sequence .


II. naming conventions for algorithms

The standard library uses two important algorithm naming and overloading specifications:

1 The first pattern contains an algorithm for testing the elements within the input range

2 another pattern applied to an algorithm that sorts elements in the input range is "not very understanding"


1 , difference the algorithm version number with a value or a predicate function parameter

very many algorithms implement their functions by examining the elements within their input range. Most of the algorithms will provide a second version number of the function , the consent program Ape to provide a comparison or test function substitution operator (< or = =) the use of .

    Sort (beg,end);    With an additional shape, representing the different arithmetic sort (beg,end,comp) used for ordering the elements    ;

the algorithm that checks for the specified value is used by default == operator.

The system provides an additional named (rather than overloaded) version number for this type of algorithm:

    Find (beg,end,val);    Find_if (beg,end,pred);

an algorithm with a predicate function, with the name of a _if suffix. Of the find_if algorithm is used to find an element that causes the predicate function pred to return a value other than 0.


2 , whether the difference implements the copy algorithm version number

/span> By default , these algorithms will again arrange the elements write back its input range . The standard library also provides additional named version numbers for these algorithms ,

The algorithm for this version number adds the _copy suffix to the name :

    Reverse (beg,end);    Reverse_copy (beg,end,dest);

P360 exercise 11.27    //The functions of these algorithms are described in terms of their names and formal references only.    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! =, ++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 << End l;    }}

Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.

C + + Primer Learning notes _45_stl Practice and Analysis (19)--building routine algorithm

Related Article

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.