C++ 实现Python 列表list 的两种方法

1、vector里面放多种参数。在C++中,如果你想要在std::vector中存储不同类型的参数,你可以使用std::any(C++17及以上)或std::variant(C++17以前的版本需要使用Boost库或者C++17及以上标准)。以下是使用std::vector<std::any>的例子:

cpp 复制代码
#include <iostream>
#include <vector>
#include <any>

int main() {
    std::vector<std::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_cast<int>(item) << std::endl;
        }
        else if (item.type() == typeid(const char*)) {
            std::cout << std::any_cast<const char*>(item) << std::endl;
        }
        else if (item.type() == typeid(double)) {
            std::cout << std::any_cast<double>(item) << std::endl;
        }
    }

    return 0;
}

2、接下来的代码使用C++11标准

cpp 复制代码
#include <iostream>
#include <vector>
#include <typeinfo>
#include <string>


class PyType {
private:
    std::string valueType;
    void* value;

public:
    template <typename T>
    PyType(T __value__) {
        valueType = typeid(__value__).name();
        value = new T(__value__);
    }

    template <typename T>
    T getValue() {
        return *static_cast<T*>(value);
    }

    std::string getType() {
        return valueType;
    }
};

class PyList {
private:
    std::vector<PyType> values;

public:

    PyList() {
    }


    //void init(auto initvalues, ...) {
    //
    //}

    template <typename T>
     void append(T value) {
        values.push_back(PyType(value));
    }

    template <typename T>
    T get(int index) {
        return values[index].getValue<T>();
    }

    std::string getType(int index) {
        return values[index].getType();
    }

    int getlength() {
        return(values.size());
    }
};

int main() {
    PyList mylist;

    mylist.append<int>(10);
    mylist.append<std::string>("Hello");
    mylist.append<double>(3.14);
    mylist.append<std::string>(" Hello World! ");

    for (int i = 0; i < 100; i++) {
        mylist.append<int>(i);
    }

    // std::cout << "Element at index 0: " << mylist.get<int>(0) << std::endl;
    // std::cout << "Element at index 1: " << mylist.get<std::string>(1) << std::endl;
    // std::cout << "Element at index 2: " << mylist.get<double>(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.get<int>(i);
        }
        else if (typeofValue == "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >") {
            std::cout << mylist.get<std::string>(i);
        }
        else if (typeofValue == "double") {
            std::cout << mylist.get<double>(i);
        }
        //添加换行符
        std::cout << std::endl;
    }


    return 0;
} 
相关推荐
m0_736919102 分钟前
C++中的策略模式实战
开发语言·c++·算法
子春一2 分钟前
Flutter for OpenHarmony:构建一个智能长度单位转换器,深入解析 Flutter 中的多字段联动、输入同步与工程化表单设计
开发语言·javascript·flutter
孞㐑¥3 分钟前
算法—位运算
c++·经验分享·笔记·算法
从此不归路9 分钟前
Qt5 进阶【9】模型-视图框架实战:从 TableView 到自定义模型的一整套落地方案
开发语言·c++·qt
人工智能AI技术10 分钟前
【Agent从入门到实践】44 监控与日志:添加监控指标、日志记录,方便问题排查
人工智能·python
人道领域20 分钟前
javaWeb从入门到进阶(SpringBoot基础案例2)
java·开发语言·mybatis
Stack Overflow?Tan9021 分钟前
c++constexpr
开发语言·c++
2301_8174973322 分钟前
自然语言处理(NLP)入门:使用NLTK和Spacy
jvm·数据库·python
雨季66631 分钟前
Flutter 三端应用实战:OpenHarmony 简易数字累加器开发指南
开发语言·flutter·ui·ecmascript
晚风吹长发31 分钟前
初步了解Linux中的信号捕捉
linux·运维·服务器·c++·算法·进程·x信号