121.類模板當做參數
阿新 • • 發佈:2018-03-21
post 值引用 stream OS color bsp main tor cout
1 #include <iostream> 2 #include <vector> 3 #include <list> 4 #include <string> 5 using namespace std; 6 7 template <class T> 8 class men 9 { 10 public: 11 men() 12 { 13 14 } 15 ~men() 16 { 17 18 } 19 20 T getage() 21 { 22 returnage; 23 } 24 25 T getname() 26 { 27 return name; 28 } 29 30 //&&使得可以右值引用 31 void setage(T &&a) 32 { 33 age = a; 34 } 35 36 void setname(T &&n) 37 { 38 name = n; 39 } 40 41 private: 42 T name; 43 T age;44 }; 45 46 //聲明把模板類當做一個參數,T1是一個模板類 47 template<template<class T>class T1> 48 class people 49 { 50 public: 51 T1<int> s1; 52 T1<string> s2; 53 people() 54 { 55 56 } 57 ~people() 58 { 59 60 } 61 }; 62 63 void main() 64 { 65 //vector<int> myint;66 ////二維數組 67 //vector<vector<int>> myint2; 68 69 people<men> my; 70 71 my.s1.setage(50); 72 cout << my.s1.getage() << endl; 73 cin.get(); 74 }
121.類模板當做參數