关键点
- tenpureto是用来将结构体和函数的“类型”进行泛化的功能
- “执行类似处理但使用的类型不同的函数”函数模板可以使用[函数模板]来进行汇总
- 函数模板的声明
template <typename テンプレート引数>
返り値の型 関数名(引数の型 引数...(必要な分だけ書く)) {
// 処理内容
}
- 作为函数模板定义的函数的调用
関数名<テンプレート引数>(引数1, 引数2, ...);
- 当想将结构体的内容对类型进行泛化时克拉斯滕普雷特可以使用[函数模板]来进行汇总
- 结构体模板的声明
template <typename テンプレート引数>
struct 構造体名 {
(構造体の内容)
};
- 使用通过结构体模板所定义的结构体
構造体名<テンプレート引数>
tenpureto
tenpureto是将结构体和函数的“类型”进行泛化的功能。
函数模板
函数模板使用[函数模板]可以将“执行类似处理但使用的类型不同的函数”进行汇总。
例如,到目前为止处理过的STL的min函数以及max函数是使用函数模板定义的。
以此int也适用于类型double类型也可以以同样的方式使用。
下面,我们将解释``某个数值。
我将以x并将其平方作为示例来解释函数。
待处理的号码int类型可以写成如下。
假设您还需要一个对
// xの二乗を返す
int square_int(int x) {
return x * x;
}
这里
如果你知道double类型的数字进行平方的函数。
// xの二乗を返す
double square_double(double x) {
return x * x;
}
这些函数可以按如下方式使用。
#include <bits/stdc++.h>
using namespace std;
// xの二乗を返す
int square_int(int x) {
return x * x;
}
// xの二乗を返す
double square_double(double x) {
return x * x;
}
int main() {
int a = 3;
double b = 1.2;
cout << square_int(a) << endl;
cout << square_double(b) << endl;
}
実行結果
9 1.44
两个函数(int和double) 函数内部的描述完全一样。
通过使用函数模板功能,可以将这种“不同类型的相同处理”组合在一起。 具体来说,定义该函数如下。
// xの二乗を返す (関数テンプレート版)
template <typename T>
T square(T x) {
return x * x;
}
调用该函数的一方如下。
#include <bits/stdc++.h>
using namespace std;
// xの二乗を返す (関数テンプレート版)
template <typename T>
T square(T x) {
return x * x;
}
int main() {
int a = 3;
double b = 1.2;
cout << square<int>(a) << endl; // int版のsquare関数の呼び出し
cout << square<double>(b) << endl; // double版のsquare関数の呼び出し
}
実行結果
9 1.44
函数模板的声明
声明函数模板:
template <typename テンプレート引数>
返り値の型 関数名(引数の型 引数...(必要な分だけ書く)) {
// 処理内容
}
模板可以用作返回值类型和参数类型。
还可以采用多个模板参数。
template <typename テンプレート引数1, typename テンプレート引数2, ...(必要な分だけ書く)>
返り値の型 関数名(引数の型 引数...(必要な分だけ書く)) {
// 処理内容
}
结构成员函数也可以使用模板来定义。
使用模板调用函数
要调用使用模板定义的函数,请编写如下。
関数名<テンプレート引数>(引数1, 引数2, ...);
仅传递定义函数时指定的模板参数数量。
関数名<テンプレート引数1, テンプレート引数2, ...(必要な分だけ書く)>(引数1, 引数2, ...);
但是,如果可以推断参数的类型,则可以省略指定模板参数。
#include <bits/stdc++.h>
using namespace std;
// xの二乗を返す (関数テンプレート版)
template <typename T>
T square(T x) {
return x * x;
}
int main() {
int a = 3;
double b = 1.2;
// テンプレート引数の省略
cout << square(a) << endl; // int版のsquare関数の呼び出し
cout << square(b) << endl; // double版のsquare関数の呼び出し
}
実行結果
9 1.44
结构中的模板
定义结构时也可以使用模板。 例如,这允许您创建一个概括成员变量类型的结构。
创建一个这样的模板 它被称为克拉斯滕普雷特。
到目前为止我已经处理过了。
STL 容器(例如vector呀
您可以前往map)是使用类模板定义的。
例如vector是vector<int>作为int类型数组,
您现在可以使用vector<double>作为double类型的数组。
以下程序使用模板概括坐标值类型。
这是Point结构的示例。
#include <bits/stdc++.h>
using namespace std;
// クラステンプレートの宣言
template <typename T>
struct Point {
T x;
T y;
void print() {
cout << "(" << x << ", " << y << ")" << endl;
}
};
int main() {
// int型用のPoint構造体
Point<int> p1 = { 0, 1 };
p1.print(); // (0, 1)
// double型用のPoint構造体
Point<double> p2 = { 2.3, 4.5 };
p2.print(); // (2.3, 4.5)
}
実行結果
(0, 1) (2.3, 4.5)
结构体模板的声明
声明类模板:
template <typename テンプレート引数>
struct 構造体名 {
(構造体の内容)
};
模板参数可以用作类型名称。
还可以采用多个模板参数。
template <typename テンプレート引数1, テンプレート引数2, ...(必要な分だけ書く)>
struct 構造体名 {
(構造体の内容)
};
将结构与模板一起使用
要使用通过类模板定义的结构,请指定模板参数,如下所示。
構造体名<テンプレート引数>
在上面的示例程序中,
对于类模板Point它用作Point<int>呀
您可以前往Point<double>。
与函数模板不同,类模板的模板参数不能省略。
常量模板
函数模板和类模板都可以使用整数类型常量作为模板参数。 当您想要根据常量的使用位置切换常量时,常量模板非常有用。
作为一个具体示例,
用于获取tuple的元素get功能,bitset使用常量模板定义。
以下是在函数模板中采用常量模板参数的示例。
#include <bits/stdc++.h>
using namespace std;
// タプルのINDEX1番目とINDEX2番目を交換する関数
template <int INDEX1, int INDEX2>
void tuple_swap(tuple<int, int, int> &x) {
swap(get<INDEX1>(x), get<INDEX2>(x));
}
int main() {
tuple<int, int, int> x = make_tuple(1, 2, 3);
tuple_swap<0, 2>(x); // 1番目と3番目を交換
cout << get<0>(x) << ", " << get<1>(x) << ", " << get<2>(x) << endl;
tuple_swap<0, 1>(x); // 1番目と2番目を交換
cout << get<0>(x) << ", " << get<1>(x) << ", " << get<2>(x) << endl;
}
実行結果
3, 2, 1 2, 3, 1
以下是在类模板中采用常量模板参数的示例。
#include <bits/stdc++.h>
using namespace std;
// MODで割ったあまりをxに保持するような構造体
template <int MOD>
struct Modulo {
int x;
Modulo() {}
Modulo(int v) {
x = v % MOD;
}
// 足し算
Modulo plus(Modulo<MOD> other) {
Modulo<MOD> result;
result.x = (x + other.x) % MOD;
return result;
}
};
int main() {
Modulo<10> a(7), b(5);
auto c = a.plus(b);
cout << c.x << endl; // (5 + 7) % 10 == 2
}
実行結果
2
注意点
即使使用模板声明的函数或结构存在拼写错误或其他错误,如果程序中未使用它,也可能不会导致编译错误。 这是因为未使用的模板在编译期间会被忽略。欲了解更多信息,请阅读“关于详细故事的具体化”。
下面是函数模板中的拼写错误示例。
#include <bits/stdc++.h>
using namespace std;
struct X {
void f() {
cout << "X" << endl;
}
};
struct Y {
void f() {
cout << "Y" << endl;
}
};
template <typename T>
void call_f(T v) {
v.g(); // fと書くところをタイプミスでgと書いてしまったとする
}
int main() {
// 何もしない
}
実行結果
函数call_f原本准备调用函数f,但由于拼写错误,它正在尝试调用函数g。 虽然有错字,但是程序中并没有使用这个模板,所以编译运行正常。
如下,
当我尝试使用call_f时,我第一次收到编译错误。
#include <bits/stdc++.h>
using namespace std;
struct X {
void f() {
cout << "X" << endl;
}
};
struct Y {
void f() {
cout << "Y" << endl;
}
};
template <typename T>
void call_f(T v) {
v.g(); // fと書くところをタイプミスでgと書いてしまったとする
}
int main() {
X x;
Y y;
call_f(x); // Xのfが呼ばれてほしい
call_f(y); // Yのfが呼ばれてほしい
}
标准错误输出
./Main.cpp: In instantiation of ‘void call_f(T) [with T = X]’: ./Main.cpp:23:11: required from here ./Main.cpp:17:3: error: ‘struct X’ has no member named ‘g’ v.g(); // fと書くところをタイプミスでgと書いてしまったとする ^ ./Main.cpp: In instantiation of ‘void call_f(T) [with T = Y]’: ./Main.cpp:24:11: required from here ./Main.cpp:17:3: error: ‘struct Y’ has no member named ‘g’
在编写诸如“一点一点地编写,编译它,检查它......”之类的代码时,您需要小心。
详细信息
模板具体化
模板是一个有用的功能,可以概括类似的处理,但它们实际上只是“自动生成函数和结构的蓝图”。 例如,函数模板是“自动生成特定于每种类型的函数的功能。”
当使用通过模板定义的函数或结构时,实际上会生成实体(函数或结构),然后使用。 以这种方式从模板创建实体称为模板实例化。
模板实例化由编译器自动完成。 当程序中使用模板时,它会根据其使用方式进行实例化,因此 如果程序中从未使用过模板,则不会实例化该模板。
square函数示例
作为函数模板的具体示例引入。
我们以square函数为例。
#include <bits/stdc++.h>
using namespace std;
/*
// xの二乗を返す
int square_int(int x) {
return x * x;
}
// xの二乗を返す
double square_double(double x) {
return x * x;
}
*/
// xの二乗を返す (関数テンプレート版)
template <typename T>
T square(T x) {
return x * x;
}
int main() {
int a = 3;
double b = 1.2;
cout << square<int>(a) << endl; // int版のsquare関数の呼び出し
cout << square<double>(b) << endl; // double版のsquare関数の呼び出し
}
実行結果
9 1.44
首先,
简单地声明square的函数模板不会生成任何函数。
在程序中
当像square<int>(a)这样使用时,编译器会告诉你int为
生成square関数」(square_int函数)。
同样地
当像square<double>(b)一样使用时,double的函数(
创建square_double函数)。
square<int>(a)和
我讨厌square<double>(b)各自具体化,因此程序执行时实际调用的函数是不同的。
与
但是,square<int>(2)和
我讨厌square<int>(3)相同
对于调用int类型,将调用相同的函数。
square<int>(2)通过int对类型执行物化,
对于square<int>(3),最好想象调用已经实现的东西。
竞争性编程中经常使用的模板示例
作为竞技编程中经常使用的函数模板,
介绍chmax/chmin。
#include <bits/stdc++.h>
using namespace std;
// aよりもbが大きいならばaをbで更新する
// (更新されたならばtrueを返す)
template <typename T>
bool chmax(T &a, const T& b) {
if (a < b) {
a = b; // aをbで更新
return true;
}
return false;
}
// aよりもbが小さいならばaをbで更新する
// (更新されたならばtrueを返す)
template <typename T>
bool chmin(T &a, const T& b) {
if (a > b) {
a = b; // aをbで更新
return true;
}
return false;
}
//-------------------------------
int f(int n) {
// (適当な関数)
return n * n - 8 * n + 3;
}
int main() {
// f(1), f(2), ..., f(10)の最小値と最大値を求める
int ans_min = 1000000000;
int ans_max = 0;
for (int i = 1; i <= 10; i++) {
chmin(ans_min, f(i)); // 最小値を更新
chmax(ans_max, f(i)); // 最大値を更新
}
cout << ans_min << " " << ans_max << endl;
}
実行結果
-13 23
chmin当找到最小值时,chmax在计算最大值时很有用。
类型名称和类别
在模板声明中typename部分是
即使你把它写成class,它也有完全相同的含义。
// template <typename T>と書くのと同じ
template <class T>
T square(T x) {
return x * x;
}
// template <typename T>と書くのと同じ
template <class T>
struct Point {
T x;
T y;
void print() {
cout << "(" << x << ", " << y << ")" << endl;
}
};