STL source code analysis [Notes]

Source: Internet
Author: User

1. Static constant integer members can be directly initialized within the class.

2. Open interval after pre-closed STL iterator[)Design, directly during traversal:

For (first; first! = Last; first ++)

Use! =Instead<Or>The benefit elements do not need to occupy continuous memory space.

3. the STL space configurator in vs is in the header file <xmomory>.

Space configuration is divided into two phases:

Application Time: apply for the memory area (_ allocate function, call: Operator new), call the constructor (_ allocate function)

Release time: Call the Destructor () _ destroy to release the memory area (directly call: Operator delete)

: Operator new is similar to malloc. Unlike new, the former does not call constructor, and the latter calls constructor.

4. To design the container iterator, you must have a rich understanding of the Implementation Details of the container. Therefore, designing the container iterator at the same time can encapsulate the container.

5,Type Extraction:

C ++ supports sizeof,

Typeid in rtti properties,

Function template parameter derivation argument deduction mechanism

Version 1:

Template <Class I, Class T> <br/> void func_impl (I ITER, t) <br/>{< br/> T TMP; // t indicates the type of the object referred to by the iterator <br/> //... do some stuff <br/>}< br/> template <class I> <br/> void func (I ITER) <br/>{< br/> func_impl (ITER, * ITER); <br/>} 

The function parameter derivation mechanism can only deduce the parameter type of a function, but cannot deduce the return value type of the function.

Version 2:

Template <class T> <br/> struct myiter <br/> {<br/> typedef t value_type; // nested type <br/> T * PTR; <br/> T & operator * () const {return * PTR ;}< br/>}; <br/> template <class I> <br/> typename I :: value_type <br/> func (I ITER) <br/>{< br/> return * ITER; <br/>} 

All iterators must have a value_type definition. Trap ~

Version 3:

Use the trait technology in combination with the template-specific template partial Specialization

Convention: Each iterator of STL must define the corresponding type (Associated types) in the nested typedef mode ).

 

6. algorithm: the solution to the problem is also ~

Generalized ghost process of algorithms: abstracted Pattern

See general technology:

Http://www.generic-programming.org/languages/cpp/techniques.php

Http://www.boost.org/community/generic_programming.html

The algorithm in STL is in # include <functional>, # include <algorithm>, # include <numeric>

 

7. Copy function:

Continuous memory copy,

Discrete Random memory copy,

User-Defined type: CompilerNon-intelligent locationDetermine whether it has non-trivial assignment operator

Basic data type, with trivial assignment operator

 

First, the last iterator points to the starting position of the source memory, and the result iterator points to the target memory.

 

// Random memory copy: Compare the iterator each time to determine whether the loop ends. The speed is slow ~

For (; first! = Last; Result ++, first ++)

* Result = * First; // call the value assignment operator.

 

// Continuous memory, judge whether the loop ends by distance N, fast ~

_ Copy_d (first, last, result)

For (distance n = last-first; n> 0; -- N, ++ result, ++ first)

* Result = * First; // call the value assignment operator.

 

// For the special version: the continuous memory zone pointed by the basic data type pointer, optimized using the memmove Function

Template <t>

Inline T * _ copy_t (const T * First, const T * Last, T * result ){

Memmove (result, first, sizeof (t) * (last-first ));

Return result + (last-first );

}

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.