C + + Learning Note 32: Generics programming Development 1

Source: Internet
Author: User

Content of the Standard Template Library

    • Standard template classes: complex numbers, sequential pairs
    • Iterators
    • Standard containers: vectors, tables, stacks, queues, sets, mappings, etc.
    • Standard algorithms: Find, sort, etc.

How to use the Standard Template library style

    • "<>": Template name < data object base type > data object name;
    • Example 1:complex<double>a (1.0,2.0);
    • Example 2:pair<string,string>name ("Zhang", "san");(sequence puppet)
    • Example 3:vector<int>v (8);

Plural

General description

    • Header file: "Complex"
    • Template name:complex<>
    • Base type: Float,double,long double
    • Preferred Double,float low precision, long double deprecated

Real part and imaginary part

member functions Real () and Imag ()

Complex operations

Complex numbers all operate in a mathematical format

Cout,cin, all overloaded; in the format (REAL,IMAG)

Sequential couple

General description

    • Header file: "Utility"
    • Template name:pair<>
    • Used to represent two objects that always appear paired
    • Example 1:pair<int,double>a (1,1.0);
    • Example 2:pair<string,string>name ("Zhang", "San");

Usage rules

    • Data members Exposed: First,second
    • Example: cout << name.first << "," << Name.second;
    • Sequential couple comparison: compare first size, compare second size at same time
    • Make_pair: Auxiliary function for constructing order pairs
    • Example:pair<int,double> A; A = Make_pair (1,1.0);

Vector

The purpose of the vector

Instead of arrays, you can use vectors like arrays

Use of vectors

    • Defines the format:vector<int> V (8);//an element containing 8 integers
    • Operator[]: overloaded, using format V[i] access to element I
    • Vectors can be assigned as a whole
    • Size (): Returns the number of elements in the vector
    • Capacity: Returns the maximum number of elements that the vector currently can store
    • Clear (): removes all elements of the vector, but does not release the vector itself
    • Resize (int newsize): Re-set vector capacity

Iterators

The nature of iterators

    • Accessing data Objects in a container through iterators
    • Functions like pointers, array indexing: Obtaining the next data object with pointer subtraction and array subscript operations
    • An iterator can be a pointer, but it does not have to be a pointer or always use the address of a data object

How to use iterators

    • Declaring iterator variables
    • Use the leading operator to access the current target object pointed to by the iterator
    • Gain access to the next object using the increment operator
    • If the new iterator value exceeds the container's element range, a pointer-like value becomes null and the target object is not referenced

Classification of iterators

    • Input iterator: Provides read-only access to the object
    • Output iterator: Provides write-only access to an object
    • Forward iterator: Provides forward (incrementing) read and write access to an object
    • Bidirectional iterator: Provides read-write access to the object's forward and reverse (increment and decrement)
    • Random Access iterators: Provides random read and write access to objects

C + + Learning Note 32: Generics programming Development 1

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.