The C ++ template class interprets the minimum STL source code model.

Source: Internet
Author: User

STL is easy to understand. However, if you want to know more about the implementation of STL source code, you can design your own template class in the future.
First, you should understand and master the design method of the template class, and then recommend the C ++ template metaprogramming and STL source code analysis, which are profound and profound STL! These books can be downloaded in an electronic version of jask.

1. Class templates and STL
STL is a standard template library for C ++. Its source code programming relies entirely on Template implementation.
A class template is a class that can create different types of members according to different parameters.

2. class template Definition
Template <template parameters>
Class Name
{
Member name;
};
You can set multiple parameters as needed.

3. STL is the best open source instance for template applications.
On the basis of understanding the basic usage of STL, understanding the design of the template class will be more profound. On the basis of understanding the design of the template class, you can design a container similar to STL that you need.
For example, the stack template example is provided.

4. A simple stack template instance
// Template of stack
Template
Class Stack
{
Public:
Stack (INT size = 10 );
~ Stack ();
Bool isfull ();
Bool isempty ();
Bool push (const T &);
T POP ();
PRIVATE:
Int m_size;
Int m_top;
T * m_space;
};

Template
STACK: Stack (INT size)
{
This-> m_size = size;
M_space = new T (size );
M_top = size;
}

Template
STACK ::~ Stack ()
{
Delete [] m_space;
}

Template
Bool Stack: isfull ()
{
Return m_top = 0;
}

Template
Bool Stack: isempty ()
{
Return m_top = m_size;
}

Template
Bool Stack: Push (const T & element)
{
If (! Isfull ())
{
M_space [-- m_top] = element;
Return true;
}
Return false;
}

Template
T Stack: Pop ()
{
Return m_space [m_top ++];
}

5. Summary
Templates and STL complement each other. When learning, pay attention to the accumulation of instance development and Template Class components.
Template Class development is an important technical means for developing C ++ components.

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.