做网站先做ue,wordpress 教学视频,出口网站平台,百度快速收录教程1、vector里面放多种参数。在C中#xff0c;如果你想要在std::vector中存储不同类型的参数#xff0c;你可以使用std::any#xff08;C17及以上#xff09;或std::variant#xff08;C17以前的版本需要使用Boost库或者C17及以上标准#xff09;。以下是使用std::vector中如果你想要在std::vector中存储不同类型的参数你可以使用std::anyC17及以上或std::variantC17以前的版本需要使用Boost库或者C17及以上标准。以下是使用std::vectorstd::any的例子
#include iostream
#include vector
#include anyint main() {std::vectorstd::any vec;vec.push_back(42);vec.push_back(hello);vec.push_back(3.14);for (const auto item : vec) {if (item.type() typeid(int)) {std::cout std::any_castint(item) std::endl;}else if (item.type() typeid(const char*)) {std::cout std::any_castconst char*(item) std::endl;}else if (item.type() typeid(double)) {std::cout std::any_castdouble(item) std::endl;}}return 0;
}
2、接下来的代码使用C11标准
#include iostream
#include vector
#include typeinfo
#include stringclass PyType {
private:std::string valueType;void* value;public:template typename TPyType(T __value__) {valueType typeid(__value__).name();value new T(__value__);}template typename TT getValue() {return *static_castT*(value);}std::string getType() {return valueType;}
};class PyList {
private:std::vectorPyType values;public:PyList() {}//void init(auto initvalues, ...) {////}template typename Tvoid append(T value) {values.push_back(PyType(value));}template typename TT get(int index) {return values[index].getValueT();}std::string getType(int index) {return values[index].getType();}int getlength() {return(values.size());}
};int main() {PyList mylist;mylist.appendint(10);mylist.appendstd::string(Hello);mylist.appenddouble(3.14);mylist.appendstd::string( Hello World! );for (int i 0; i 100; i) {mylist.appendint(i);}// std::cout Element at index 0: mylist.getint(0) std::endl;// std::cout Element at index 1: mylist.getstd::string(1) std::endl;// std::cout Element at index 2: mylist.getdouble(2) std::endl;for (int i 0; i mylist.getlength(); i) {//判断类型并且将类型与内容打印在屏幕上std::string typeofValue mylist.getType(i);std::cout type of index[ i ] is typeofValue value is ;if (typeofValue int) {std::cout mylist.getint(i);}else if (typeofValue class std::basic_stringchar,struct std::char_traitschar,class std::allocatorchar ) {std::cout mylist.getstd::string(i);}else if (typeofValue double) {std::cout mylist.getdouble(i);}//添加换行符std::cout std::endl;}return 0;
}