C++ template函数模板
生活随笔
收集整理的這篇文章主要介紹了
C++ template函数模板
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
定義一個(gè)函數(shù)模板,任何類型變量均可使用
#include<iostream> #include<string>template<class T> //或者使用template<typename T> void swap(T &a, T &b) {T temp = a;a = b;b = temp; }int main() {int i1 = 100;int i2 = 200;std::cout << "交換前:" << i1 << " " << i2 << std::endl;swap(i1, i2);std::cout << "交換后:" << i1 << " " << i2 << std::endl;std::string s1 = "C++是世界上最好的語言";std::string s2 = "你好世界";std::cout << "交換前:" << s1 << " " << s2 << std::endl;swap(s1, s2);std::cout << "交換后:" << s1 << " " << s2 << std::endl;system("pause"); }運(yùn)行結(jié)果
交換前:100 200
交換后:200 100
交換前:C++是世界上最好的語言 你好世界
交換后:你好世界 C++是世界上最好的語言
請(qǐng)按任意鍵繼續(xù). . .
總結(jié)
以上是生活随笔為你收集整理的C++ template函数模板的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 牛客网_PAT乙级1008_锤子剪刀布
- 下一篇: C++ template类模板实现栈 p