c++函数模板、类模板(含模板特化)
作者:野牛程序员:2024-01-03 15:06:34 C++阅读 2716
函数模板和类模板是 C++ 中用于实现泛型编程的重要特性。它们允许编写通用的代码,以便适用于多种数据类型而无需重复编写相似的代码。
函数模板(Function Templates):
// 函数模板示例
template <typename T>
T add(T a, T b) {
return a + b;
}
// 函数模板的使用
int result1 = add(5, 10); // 编译器会自动推导出 T 为 int
double result2 = add(3.14, 2.71); // 编译器会自动推导出 T 为 double类模板(Class Templates):
// 类模板示例
template <typename T>
class Pair {
public:
Pair(T first, T second) : first(first), second(second) {}
T getFirst() const {
return first;
}
T getSecond() const {
return second;
}
private:
T first;
T second;
};
// 类模板的使用
Pair<int> intPair(5, 10); // 实例化 Pair 类模板,其中 T 被替换为 int
Pair<double> doublePair(3.14, 2.71); // 实例化 Pair 类模板,其中 T 被替换为 double模板特化(Template Specialization):
// 模板特化示例
template <typename T>
class MyContainer {
public:
MyContainer(T value) : value(value) {}
void display() {
std::cout << "General Template: " << value << std::endl;
}
private:
T value;
};
// 针对特定类型的模板特化
template <>
class MyContainer<std::string> {
public:
MyContainer(std::string value) : value(value) {}
void display() {
std::cout << "Specialized Template for strings: " << value << std::endl;
}
private:
std::string value;
};
// 使用模板和模板特化
MyContainer<int> intContainer(42);
MyContainer<std::string> strContainer("Hello");
intContainer.display(); // 输出: General Template: 42
strContainer.display(); // 输出: Specialized Template for strings: Hello这些例子展示了 C++ 中函数模板、类模板以及模板特化的基本用法。
#include <iostream>
#include <string>
// 函数模板示例
template <typename T>
T add(T a, T b) {
return a + b;
}
// 类模板示例
template <typename T>
class Pair {
public:
Pair(T first, T second) : first(first), second(second) {}
T getFirst() const {
return first;
}
T getSecond() const {
return second;
}
private:
T first;
T second;
};
// 模板特化示例
template <typename T>
class MyContainer {
public:
MyContainer(T value) : value(value) {}
void display() {
std::cout << "General Template: " << value << std::endl;
}
private:
T value;
};
// 模板特化 - 针对 std::string 类型
template <>
class MyContainer<std::string> {
public:
MyContainer(std::string value) : value(value) {}
void display() {
std::cout << "Specialized Template for strings: " << value << std::endl;
}
private:
std::string value;
};
int main() {
// 函数模板的使用
int result1 = add(5, 10);
double result2 = add(3.14, 2.71);
std::cout << "Function Template Results:" << std::endl;
std::cout << "Result 1: " << result1 << std::endl;
std::cout << "Result 2: " << result2 << std::endl;
// 类模板的使用
Pair<int> intPair(5, 10);
Pair<double> doublePair(3.14, 2.71);
std::cout << "\\nClass Template Results:" << std::endl;
std::cout << "Int Pair: (" << intPair.getFirst() << ", " << intPair.getSecond() << ")" << std::endl;
std::cout << "Double Pair: (" << doublePair.getFirst() << ", " << doublePair.getSecond() << ")" << std::endl;
// 模板特化的使用
MyContainer<int> intContainer(42);
MyContainer<std::string> strContainer("Hello");
std::cout << "\\nTemplate Specialization Results:" << std::endl;
intContainer.display();
strContainer.display();
return 0;
}野牛程序员教少儿编程与信息学奥赛-微信|电话:15892516892

