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 return age; 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 }