在CentOS系统中使用C++模板,你需要遵循以下步骤:
sudo yum install gcc-c++
main.cpp。在这个文件中,你可以编写使用模板的C++代码。例如:#include <iostream>
template <typename T>
T add(T a, T b) {
return a + b;
}
int main() {
int a = 10;
int b = 20;
std::cout << "Sum of integers: " << add<int>(a, b) << std::endl;
double c = 3.14;
double d = 6.28;
std::cout << "Sum of doubles: " << add<double>(c, d) << std::endl;
return 0;
}
g++ -o main main.cpp
这将生成一个名为main的可执行文件。
./main
你应该会看到以下输出:
Sum of integers: 30
Sum of doubles: 9.42
这就是在CentOS系统中使用C++模板的方法。你可以根据需要修改源代码,添加更多的模板函数和类,并按照相同的步骤编译和运行程序。