都知道C++STL容器,只要定义好之后就只能存定义的类型,不能放入别的类型。
以vector容器为例,定义好只存int类型,其它类型将无法存入。
1 2 3 |
vector<int> vecint; vecint.push_back(10); vecint.push_back('s'); //错误,只能放入int类型 |
哪容器里有没有办法可以存不同类型呢?
办法肯定是有的,C++就是这点好,什么都可以做。
下面讲个我想到的一个方法,估计还有很多方法。
先定义一个结构AnyData,结构中定义状态Eflag(用于判断是存的是什么类型)和union(里面存数据)。
结构AnyData定义构造函数,为每种类型定义一个。
getType()用于获取当前数据类型。
getVal()重载函数用于获取实际数据值。
用stl vector容器为例,实现存放不同类型完整代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
// 梁笔记 // https://zouzhongliang.com #include <iostream> #include <vector> using namespace std; enum EType{E_bool, E_char, E_short, E_int, E_long, E_float, E_double}; struct AnyData { EType Eflag; union { bool bVal; char cVal; short sval; int ival; long lval; float fval; double dval; }; AnyData(bool val){ Eflag = E_bool; bVal = val; } AnyData(char val){ Eflag = E_char; cVal = val; } AnyData(short val){ Eflag = E_short; sval = val; } AnyData(int val){ Eflag = E_int; ival = val; } AnyData(long val){ Eflag = E_long; lval = val; } AnyData(float val){ Eflag = E_float; fval = val; } AnyData(double val){ Eflag = E_double; dval = val; } EType getType(){ return Eflag; } void getVal(bool& val){ if (Eflag == E_bool) val = bVal; } void getVal(char& val){ if (Eflag == E_char) val = cVal; } void getVal(short& val){ if (Eflag == E_short) val = sval; } void getVal(int& val){ if (Eflag == E_int) val = ival; } void getVal(long& val){ if (Eflag == E_long) val = lval; } void getVal(float& val){ if (Eflag == E_float) val = fval; } void getVal(double& val){ if (Eflag == E_double) val = dval ; } friend ostream& operator<<(ostream& out, AnyData& data); }; ostream& operator<<(ostream& out, AnyData& data){ switch(data.Eflag){ case E_bool: out<< data.bVal; break; case E_char: out<< data.cVal; break; case E_short: out<< data.sval; break; case E_int: out<< data.ival; break; case E_long: out<< data.lval; break; case E_float: out<< data.fval; break; case E_double: out<< data.dval; break; } return out; } int main() { vector<AnyData> vecAnyData; vecAnyData.push_back(AnyData(false)); vecAnyData.push_back(AnyData('q')); vecAnyData.push_back(AnyData((short)12)); vecAnyData.push_back(AnyData(12)); vecAnyData.push_back(AnyData((float)2.1)); vecAnyData.push_back(AnyData(1.0)); for(unsigned int i=0;i<vecAnyData.size();i++){ cout<<vecAnyData[i]<<endl; } return 0; } |
输出结果:
1 2 3 4 5 6 |
0 q 12 12 2.1 1 |
Useful stuff. Cheers! diflucan generic
Fantastic material. Regards! fluconazole 200 mg
Fine tips. Many thanks! diflucan for yeast infection
Point effectively utilized.. fluconazole cream
Excellent tips. With thanks! top rated canadian pharmacies online
Kudos! Lots of knowledge!
valganciclovir
Really many of good data. prednisolone acetate
Really plenty of good tips! Buy Kamagra By Paypal
Terrific forum posts. Many thanks! canadian pharmacies-24h
This is nicely said! . methocarbamol 750 mg
Fine material. Appreciate it! propecia
You made your point. prescription drugs from canada online
Helpful info. Cheers. retin a
Tips very well taken.! finasterid
Superb posts. Kudos. canadadrugstore365
You suggested that wonderfully. cbd oil benefits
Wonderful information. With thanks. what is cbd oil
Thanks, Fantastic information! canada pharmaceuticals online
You mentioned this effectively. augmentin 875