C++ templates, STL (Standard Template Library)

The main content of this article is the introduction of function templates , class templates , and STL in C++ .

I hope it will be helpful to C++ lovers, the content is substantial and dry , like + bookmark to prevent you from finding it!

Thanks again to every reader and friends who are learning programming for coming!

For more high-quality content, please click Yijia:

C++ Collection Library: Rebirth of C++ Departure (The average quality score of the article is 93)

Table of contents

 

1. Template

(1) Function template

(2) class template

2. Know STL

(1) What is STL

(2) Six major components of STL

(3) How to learn STL


1. Template

(1) Function template

Suppose a scenario : When we want to write a swap() function, should we write it like this

#include<iostream>
using namespace std;
void swap(int& a,int& b)
{
	int tmp = a;
	a = b;
	b = tmp;
}
int main()
{
	int a = 2;
	int b = 3;
	swap(a, b);
	return 0;
}

Then when we want to exchange two variables of char type at the same time , do we have to write like this

#include<iostream>
using namespace std;
void swap(int& a,int& b)
{
	int tmp = a;
	a = b;
	b = tmp;
}
void swap(char& c, char& d)
{
	char tmp = c;
	c = d;
	d = tmp;
}
int main()
{
	int a = 2;
	int b = 3;
	swap(a, b);
	char c = 's';
	char d = 'b';
	swap(c, d);
	return 0;
}

Writing in this way constitutes the overloading of the function , so that the corresponding type matches the corresponding function, and finds the function of the appropriate type every time it is exchanged.

Although function overloading solves the need to call the same functional function with different types of formal parameters to a certain extent , every time there is a new parameter type requirement, the programmer needs to write an overloaded function by himself.

Since the functions of these two swap() functions are exactly the same, but the parameter types are different , it is tm troublesome to write another copy.

At this time, the template that meets the needs will appear on the stage.

Function template definition: A function template represents a family of functions. The function template has nothing to do with the type. It is parameterized when used, and a specific type version of the function is generated according to the type of the actual parameter .

Function template format: template<class T1,class T2…>

                          Function parameters (T1 a, T2 b)

Just looking at this format may be a bit abrupt, so let's use the swap function to feel the charm of function templates :

#include<iostream>
using namespace std;
template <class T>//函数模板
void newswap(T& a,T& b)
{
	T tmp = a;
	a = b;
	b = tmp;
}
int main()
{
	int a = 2;
	int b = 3;
	newswap(a, b);
	char c = 's';
	char d = 'b';
	newswap(c, d);
	return 0;
}

Using function templates, the code is significantly shorter, and the newswap function can handle different types of parameters with ease .

Principle: A function template is a blueprint . It is not a function itself, but a mold for the compiler to generate a specific type of function by using it . So in fact, the template is to hand over the repetitive things that we should have done to the compiler .

Template instantiation: The process of using a template to generate a function of a specific concrete type is called template instantiation .

Implicit instantiation: Let the compiler deduce the type of T of the template parameter by itself . For example, in the above figure, when the swap function is called, the compiler can instantiate the template parameter T through the type of the actual parameter , thereby generating a function of type T for us to call .

But the compiler will report an error if it cannot deduce it:

Explicit instantiation: The programmer manually controls the type of T when the template function is called.

#include<iostream>
using namespace std;
template <class T>
void newswap(T& a,T& b)
{
	T tmp = a;
	a = b;
	b = tmp;
}
int main()
{
	int a = 2;
	int b = 3;
	newswap<int>(a, b);
	return 0;
}

 

This is the explicit instantiation of the template . After the programmer explicitly instantiates the template, the compiler will not deduce the type of the template parameter T.

(2) class template

The general idea of ​​a class template is the same as a function template.

It is also a class that has to write classes with different parameter types just because of the different types of parameters . Then apply the template attribute to the class. It makes a class template can supply the class requirements of different types of parameters at the same time, which is very easy to use.

2. Know STL

(1) What is STL

For C++ enthusiasts, STL can be said to be the most commonly used tool library in the C++ standard library. It is not only a reusable component library, but also a software framework including data structures and algorithms . Proficiency in using STL and learning the bottom layer of STL are essential.

(2) Six major components of STL

(3) How to learn STL

The first level: skilled use of STL

The second realm: understanding the bottom layer of STL

The Third Realm: Extending the STL

Summary: It can be used, it can be reasoned, and it can be expanded.


The next article begins to enter the in-depth study of STL in C++.

Today's content is shared here. If it is helpful to everyone, remember to like and collect it so as not to get lost. Subscribe to the C++ column below . Continue to explode the practical knowledge of C++.

Guess you like

Origin blog.csdn.net/2301_76144863/article/details/132228501