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;
} 
相关推荐
blasit5 小时前
笔记:Qt C++建立子线程做一个socket TCP常连接通信
c++·qt·tcp/ip
IVEN_6 小时前
只会Python皮毛?深入理解这几点,轻松进阶全栈开发
python·全栈
Ray Liang7 小时前
用六边形架构与整洁架构对比是伪命题?
java·python·c#·架构设计
AI攻城狮7 小时前
如何给 AI Agent 做"断舍离":OpenClaw Session 自动清理实践
python
千寻girling7 小时前
一份不可多得的 《 Python 》语言教程
人工智能·后端·python
AI攻城狮10 小时前
用 Playwright 实现博客一键发布到稀土掘金
python·自动化运维
曲幽11 小时前
FastAPI分布式系统实战:拆解分布式系统中常见问题及解决方案
redis·python·fastapi·web·httpx·lock·asyncio
孟健1 天前
Karpathy 用 200 行纯 Python 从零实现 GPT:代码逐行解析
python
码路飞1 天前
写了个 AI 聊天页面,被 5 种流式格式折腾了一整天 😭
javascript·python