STL Learning Notes (chapter II Introduction to C + + and its standard libraries)

Source: Internet
Author: User
Tags bitset

This chapter focuses on some of the most important new language features related to the C + + standard library

Template (Templates)

Almost everything in the library is designed as a template form. The so-called templates is a function or category written for "one or more of the gender that is not yet clear."

Here is a typical example

template<classconst t& max (const t& A,const t& B){       returnb:a; {

The template does not compile at one time, generating the code for the community and all types, but rather for compiling a type (or group of) that is used. So you have to provide one of its works before you can invoke it.

Nontype templates parameter (non-type template parameter)

Type (type) can be used as the template parameter, and non-type (Nontype) can also be used as the template parameter. For example, you can specify the number of bits in the standard category bitset<> as the template parameter. The following defines two bits-made containers, 32 bits space and 50 bits space, respectively.

bitset<> flags32;bitset< >flags50;

Default templates Parameters (default template parameters)

Template classes can have default parameters. For example, the following declaration allows you to declare a MyClass object using one or two template parameters.

Template <class T,class container = vector<t> >class MyClass;

Keyword TypeName

The keyword typename is used as the identification symbol before the type. Consider the following example:

Template <class t>class  myclass{       *ptr;       ...};

Here, TypeName points out that subtype is a type defined in class T, so PTR is a pointer to the T::subtype type.

If no keyword Typename,subtype will be treated as a static member, then T::subtype *ptr will be interpreted as the product of the value subtype and PTR within the type T.

TypeName can also be used in the template declaration to override the keyword class:

Template <typename t>class MyClass;

Member Template (Member templates)

The class member function can be a template, but such a member template cannot be virtual or have default parameters, such as:

class myclass{...       <class t>      void  F (T);};

Here Myclass::f declares a set of member functions, using any type parameter. This feature is typically used to provide automatic type conversions for members in the template classes.

If you use a class template, its type must exactly match the type of the object provided by the caller:

Template <classT>classmyclass{Private: T value;  Public:        voidAssignConstmyclass<t>& x) {value=X.value;} ...  };voidf () {MyClass<Double>D; MyClass<int>i; D.assign (d)//OKD.assign (i)//ERROR}
View Code

The member template function can relax the rule "must match exactly". As long as the type can be assigned, it can be used as a function parameter.

Template <classT>classmyclass{Private: T value;  Public: Template<classX>voidAssignConstmyclass<x>& X) {value=x.getvalue ();} T GetValue ()Const{returnvalue;}};voidf () {MyClass<Double>D; MyClass<int>i;   D.assign (d); //OKD.assign (i);//OK}
View Code

Because the assign () parameter x and *this are not the same type, you cannot directly access the private and protected members of the myclass<>, so provide something like GetValue ().

Template Constructor is a special form of member template. The template constructor does not obscure implicit copy constructor. If the type is fully matched, implicit copy constructor will be generated and called.

Template <classT>classmyclass{ Public: Template<classU>MyClass (Constmyclass<u>&x); ...};voidf () {MyClass<Double>xd; MyClass<Double> Xd2 (XD)//calls built-in copy constructormyclass<int> XI (XD)//calls template constructor}
View Code

Display initialization of basic type (EXPLICIT initialization)

If you use explicit constructor calling syntax with no parameters, the basic type is initialized to 0:

int i1;            // Undefined value int i2=int();   // initialized with zero

This feature ensures that when we write the template program code, any type has an exact initial value:

Template <class t>void  f () {    T x=t ();    ...}

Keyword explicit

By using the keyword explicit, we can prohibit the "single-argument constructor" from being used for automatic type conversions.

class stack{    explicit Stack (int  size);    ...};

Without explicit, this constructor has the ability to automatically convert an int into a stack. In this case, you can assign an integer value to the stack without causing any problems:

Stack s;...s=+/  /Create a new Stack for elements and assigns it to S

Using the explicit constructor causes the above assignment to cause a compilation error.

STL Learning Notes (chapter II Introduction to C + + and its standard libraries)

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.