c++--Study Notes 013

Source: Internet
Author: User

Climbing the C + + Peak

The third chapter 8th

STL Standard Template Library

Algorithm + container + iterator =stl

Algorithm: Algorithm

Container: Container

Iterators: Iterator

Container Adapter: Container Adaptor

Function object: Functor

STL Containers provide the convenience of reusing existing implementations to construct their own data structures under a particular type.

The STL contains many general-purpose algorithms in computer science.

The algorithm uses iterators to locate and manipulate elements in the container. Without iterators, algorithms and containers cannot interact with each other.

STL is organized in a number of header files and namespaces, the drug wants to use the STL in the program more simple, just introduce the corresponding header file, using the corresponding namespaces can be


650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/72/93/wKioL1XnWzXgJkw6AAQC83eJsmU612.jpg "title=" capture. PNG "alt=" Wkiol1xnwzxgjkw6aaqc83ejsmu612.jpg "/>

function Template---function box

Although the algorithm handles different types of data, the algorithm itself is the same, and the function template can be used to simplify the work and reuse the code.

Syntax format for function templates:

Template <typename identifiers >

return value type function name (formal parameter list)

{

function body

}

For example:

Custom comparison function templates

T is the parameter of the function template

Template<typename t>

T Mymax (const T a,const t B)

{

Return a>b? A:B;

}

Identifiers defined with TypeName are the abstract data types in a function template. We can use the abstract data type as a template instead of the actual data type.

int _tmain (int argc,_tchar* argv[])

{

Using function templates to compare integers

int na=2;

int nb=5;

wcout<<na<< "and" <<nb<< "in the larger is" <<mymax (NA,NB) <<endl;//dynamically generated template function int Mymax (int, int

Compare floating-point numbers using function templates

float fa=2.2;

float fb=5.5;

wcout<<fa<< "and" <<fb<< "in the larger is" <<mymax (FA,FB) <<endl;//dong Dai generated template function float Mymax ( Float,float)

return 0;

}

T->int

T->float

When invoking a template function, the compiler automatically infers the type parameters of the template function based on the parameter type of the calling template function.

To explicitly invoke a specific version of a template function

Displays the type parameter that indicates that the Mymax () template function is of type string, which will call the Mymax () function of the string type version

String strmax=mymax<string> ("Chen", "Jia");

cout<< "Larger string is" <<strmax<<endl;

The result above is that Jia is a larger string, but we don't want this result

In this case, we want the Mymax () function to return a long string of two strings, which requires the template function to be specialized to implement a specific type of template function.

Template functions that implement a specific type of string using template specificity

Template<>

String mymax<string> (const string A,const string B)

{

Return A.length () >b.length ()? A:B;

}

After defining the above function template, start calling

Scenario 1: By default, the mymax<char*> version is called

The character of the string is compared, and the larger string is "Jia"

String Strmax=mymax ("Chen", "Jia");

cout<< "Use normal version, larger string is" <<strmax<<endl;

Scenario 2: Display the specified template parameter type, calling the mymax<string> version after template specificity

Compares the length of a string to a longer string "Chen"

strmax= mymax<string> ("Chen", "Jia");

cout<< "Use template-specific version, longer string is" <<strmax<<endl;

Class template

Syntax format for class templates

Template <typename identifiers >

Class name

{

Definition of Class

}


c++--Study Notes 013

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.