卓姆比尼人免安装绿色版
637M · 2025-09-26
在 C++ 中,函数模板是一种通用函数定义,允许你编写可以处理多种数据类型的函数。通过使用模板,你可以避免为每种数据类型编写重复的代码,从而提高代码的复用性和可维护性。
模板就是建立通用的模具,大大提高复用性。
模板的特点:
C++另一种编程思想称为 ==泛型编程== ,主要利用的技术就是模板
函数模板的定义使用 template
关键字,后跟模板参数列表。模板参数可以是类型参数(如 typename T
)或非类型参数(如 int N
)。
语法
template <typename T>
返回类型 函数名(参数列表) {
// 函数体
}
示例 1:交换两个值
#include <iostream>
// 定义函数模板
template <typename T>
void swap(T &a, T &b) {
T temp = a;
a = b;
b = temp;
}
int main() {
int x = 10, y = 20;
std::cout << "Before swap: x = " << x << ", y = " << y << std::endl;
swap(x, y); // 调用模板函数
std::cout << "After swap: x = " << x << ", y = " << y << std::endl;
double a = 1.5, b = 2.5;
std::cout << "Before swap: a = " << a << ", b = " << b << std::endl;
swap(a, b); // 调用模板函数
std::cout << "After swap: a = " << a << ", b = " << b << std::endl;
return 0;
}
示例 2:返回最大值
#include <iostream>
// 定义函数模板
template <typename T>
T max(T a, T b) {
return (a > b) ? a : b;
}
int main() {
int x = 10, y = 20;
std::cout << "Max of " << x << " and " << y << " is " << max(x, y) << std::endl;
double a = 1.5, b = 2.5;
std::cout << "Max of " << a << " and " << b << " is " << max(a, b) << std::endl;
return 0;
}
总结:
函数模板可以接受多个类型参数。示例
#include <iostream>
// 定义函数模板
template <typename T1, typename T2>
void printPair(T1 a, T2 b) {
std::cout << "Pair: (" << a << ", " << b << ")" << std::endl;
}
int main() {
printPair(10, 20.5); // 输出: Pair: (10, 20.5)
printPair("Hello", 42); // 输出: Pair: (Hello, 42)
return 0;
}
注意事项:
//利用模板提供通用的交换函数
template<class T>
void mySwap(T &a, T &b) {
T temp = a;
a = b;
b = temp;
}
//1、自动类型推导,必须推导出一致的数据类型T,才可以使用
void test1() {
int a = 10;
int b = 20;
char c = 'c';
mySwap(a,b); // 正确,可以推导出一致的T
//mySwap(a,c); // 错误,推导不出一致的T类型
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "c = " << c << endl;
}
template<class T>
void func() {
cout << "func 调用" << endl;
}
void test2() {
//func(); //错误,模板不能独立使用,必须确定出T的类型
func<int>(); //利用显示指定类型的方式,给T一个类型,才可以使用该模板
}
int main() {
test1();
test2();
return 0;
}
总结: 使用模板时必须确定出通用数据类型T,并且能够推导出一致的类型
函数模板的特化是指为特定类型提供特殊的实现。
示例
#include <iostream>
// 通用模板
template <typename T>
void print(T value) {
std::cout << "Generic: " << value << std::endl;
}
// 特化版本(针对 const char*)
template <>
void print<const char*>(const char* value) {
std::cout << "Specialized: " << value << std::endl;
}
int main() {
print(10); // 调用通用模板
print("Hello"); // 调用特化版本
return 0;
}
函数模板可以与普通函数重载。编译器会优先选择最匹配的函数。
示例
#include <iostream>
// 普通函数
void print(int value) {
std::cout << "Overloaded: " << value << std::endl;
}
// 函数模板
template <typename T>
void print(T value) {
std::cout << "Template: " << value << std::endl;
}
int main() {
print(10); // 调用普通函数
print(10.5); // 调用函数模板
return 0;
}
在 C++ 中,类模板是一种通用类定义,允许你编写可以处理多种数据类型的类。通过使用类模板,你可以避免为每种数据类型编写重复的代码,从而提高代码的复用性和可维护性。
类模板的定义使用 template
关键字,后跟模板参数列表。模板参数可以是类型参数(如 typename T
)或非类型参数(如 int N
)。
语法
template <typename T>
class 类名 {
// 类成员
};
typename T
:表示一个类型参数,T
可以是任意类型(如 int
、double
、std::string
等)。T
可以像普通类型一样使用。示例 1:简单的类模板
#include <iostream>
// 定义类模板
template <typename T>
class Box {
private:
T value;
public:
Box(T v) : value(v) {}
void print() {
std::cout << "Value: " << value << std::endl;
}
};
int main() {
Box<int> intBox(10); // 实例化一个 Box<int> 对象
intBox.print(); // 输出: Value: 10
Box<double> doubleBox(3.14); // 实例化一个 Box<double> 对象
doubleBox.print(); // 输出: Value: 3.14
return 0;
}
示例 2:类模板的成员函数。类模板的成员函数可以在类外定义,但需要显式指定模板参数。
#include <iostream>
template <typename T>
class Box {
private:
T value;
public:
Box(T v);
void print();
};
// 类外定义构造函数
template <typename T>
Box<T>::Box(T v) : value(v) {}
// 类外定义成员函数
template <typename T>
void Box<T>::print() {
std::cout << "Value: " << value << std::endl;
}
int main() {
Box<int> intBox(42);
intBox.print(); // 输出: Value: 42
Box<std::string> stringBox("Hello");
stringBox.print(); // 输出: Value: Hello
return 0;
}
类模板可以接受多个类型参数。示例
#include <iostream>
template <typename T1, typename T2>
class Pair {
private:
T1 first;
T2 second;
public:
Pair(T1 f, T2 s) : first(f), second(s) {}
void print() {
std::cout << "Pair: (" << first << ", " << second << ")" << std::endl;
}
};
int main() {
Pair<int, double> p1(10, 3.14);
p1.print(); // 输出: Pair: (10, 3.14)
Pair<std::string, int> p2("Hello", 42);
p2.print(); // 输出: Pair: (Hello, 42)
return 0;
}
类模板的特化是指为特定类型提供特殊的实现。
示例
#include <iostream>
// 通用模板
template <typename T>
class Box {
private:
T value;
public:
Box(T v) : value(v) {}
void print() {
std::cout << "Generic: " << value << std::endl;
}
};
// 特化版本(针对 const char*)
template <>
class Box<const char*> {
private:
const char* value;
public:
Box(const char* v) : value(v) {}
void print() {
std::cout << "Specialized: " << value << std::endl;
}
};
int main() {
Box<int> intBox(10);
intBox.print(); // 输出: Generic: 10
Box<const char*> stringBox("Hello");
stringBox.print(); // 输出: Specialized: Hello
return 0;
}
可以为类模板的模板参数指定默认值。示例
#include <iostream>
template <typename T = int>
class Box {
private:
T value;
public:
Box(T v) : value(v) {}
void print() {
std::cout << "Value: " << value << std::endl;
}
};
int main() {
Box<> intBox(10); // 使用默认模板参数 int
intBox.print(); // 输出: Value: 10
Box<double> doubleBox(3.14); // 显式指定模板参数 double
doubleBox.print(); // 输出: Value: 3.14
return 0;
}
类模板可以嵌套在其他类或类模板中。示例
#include <iostream>
template <typename T>
class Outer {
public:
template <typename U>
class Inner {
private:
U value;
public:
Inner(U v) : value(v) {}
void print() {
std::cout << "Inner value: " << value << std::endl;
}
};
};
int main() {
Outer<int>::Inner<double> inner(3.14);
inner.print(); // 输出: Inner value: 3.14
return 0;
}
C++ 标准模板库(Standard Template Library,STL)是 C++ 标准库的一部分,提供了一系列通用的模板类和函数,用于实现常见的数据结构和算法。
STL 的核心组件包括 容器、迭代器、算法 和 函数对象。
容器是用于存储数据的模板类,分为以下几类:
vector
、list
、deque
。set
、map
、multiset
、multimap
。unordered_set
、unordered_map
。stack
、queue
、priority_queue
。迭代器是用于遍历容器中元素的对象,类似于指针。STL 提供了多种迭代器:
STL 提供了大量通用算法,用于操作容器中的数据,如排序、查找、遍历等。这些算法通过迭代器与容器交互。
函数对象是重载了 operator()
的类对象,可以像函数一样调用。STL 中的许多算法支持函数对象作为参数。
find
查找容器中指定值的第一个匹配项。
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> vec = {1, 2, 3, 4, 5};
auto it = std::find(vec.begin(), vec.end(), 3);
if (it != vec.end()) {
std::cout << "Found: " << *it << std::endl; // 输出: Found: 3
}
return 0;
}
count
统计容器中指定值的出现次数。
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> vec = {1, 2, 2, 3, 2};
int cnt = std::count(vec.begin(), vec.end(), 2);
std::cout << "Count: " << cnt << std::endl; // 输出: Count: 3
return 0;
}
for_each
对容器中的每个元素执行指定操作。
#include <iostream>
#include <vector>
#include <algorithm>
void print(int x) {
std::cout << x << " ";
}
int main() {
std::vector<int> vec = {1, 2, 3, 4, 5};
std::for_each(vec.begin(), vec.end(), print); // 输出: 1 2 3 4 5
return 0;
}
这些算法会修改容器中的元素。
copy
将一个容器的元素复制到另一个容器。
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> src = {1, 2, 3, 4, 5};
std::vector<int> dst(5);
std::copy(src.begin(), src.end(), dst.begin());
for (int i : dst) {
std::cout << i << " "; // 输出: 1 2 3 4 5
}
return 0;
}
fill
将容器中的元素填充为指定值。
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> vec(5);
std::fill(vec.begin(), vec.end(), 10);
for (int i : vec) {
std::cout << i << " "; // 输出: 10 10 10 10 10
}
return 0;
}
transform
对容器中的每个元素进行转换,并将结果存储到另一个容器。
#include <iostream>
#include <vector>
#include <algorithm>
int square(int x) {
return x * x;
}
int main() {
std::vector<int> src = {1, 2, 3, 4, 5};
std::vector<int> dst(src.size());
std::transform(src.begin(), src.end(), dst.begin(), square);
for (int i : dst) {
std::cout << i << " "; // 输出: 1 4 9 16 25
}
return 0;
}
sort
对容器中的元素进行排序。
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> vec = {5, 3, 1, 4, 2};
std::sort(vec.begin(), vec.end());
for (int i : vec) {
std::cout << i << " "; // 输出: 1 2 3 4 5
}
return 0;
}
stable_sort
稳定排序,保持相等元素的相对顺序。
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> vec = {5, 3, 1, 4, 2};
std::stable_sort(vec.begin(), vec.end());
for (int i : vec) {
std::cout << i << " "; // 输出: 1 2 3 4 5
}
return 0;
}
partial_sort
对容器中的部分元素进行排序。
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> vec = {5, 3, 1, 4, 2};
std::partial_sort(vec.begin(), vec.begin() + 3, vec.end());
for (int i : vec) {
std::cout << i << " "; // 输出: 1 2 3 5 4
}
return 0;
}
binary_search
在有序容器中查找指定值。
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> vec = {1, 2, 3, 4, 5};
bool found = std::binary_search(vec.begin(), vec.end(), 3);
std::cout << (found ? "Found" : "Not found") << std::endl; // 输出: Found
return 0;
}
lower_bound
返回第一个不小于指定值的元素位置。
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> vec = {1, 2, 3, 4, 5};
auto it = std::lower_bound(vec.begin(), vec.end(), 3);
std::cout << "Lower bound: " << *it << std::endl; // 输出: Lower bound: 3
return 0;
}
upper_bound
返回第一个大于指定值的元素位置。
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> vec = {1, 2, 3, 4, 5};
auto it = std::upper_bound(vec.begin(), vec.end(), 3);
std::cout << "Upper bound: " << *it << std::endl; // 输出: Upper bound: 4
return 0;
}
accumulate
计算容器中元素的和。
#include <iostream>
#include <vector>
#include <numeric>
int main() {
std::vector<int> vec = {1, 2, 3, 4, 5};
int sum = std::accumulate(vec.begin(), vec.end(), 0);
std::cout << "Sum: " << sum << std::endl; // 输出: Sum: 15
return 0;
}
inner_product
计算两个容器的内积。
#include <iostream>
#include <vector>
#include <numeric>
int main() {
std::vector<int> vec1 = {1, 2, 3};
std::vector<int> vec2 = {4, 5, 6};
int result = std::inner_product(vec1.begin(), vec1.end(), vec2.begin(), 0);
std::cout << "Inner product: " << result << std::endl; // 输出: Inner product: 32
return 0;
}
STL 算法提供了强大的功能,可以高效地操作容器中的数据。常用的算法包括:
find
、count
、for_each
。copy
、fill
、transform
。sort
、stable_sort
、partial_sort
。binary_search
、lower_bound
、upper_bound
。accumulate
、inner_product
。腾讯 QQ 音乐推出“网赚畅听包”会员,付费后每天看广告获取听歌权益
开源电子书管家 Calibre 8.11 发布:整合 AI 问答功能,随时解答你的提问