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;
} 
相关推荐
星恒随风21 分钟前
从0实现C++ AI大模型接入SDK(七):Ollama本地模型部署、NDJSON流式响应与LLMManager模型管理
linux·开发语言·c++·人工智能·笔记
积流成海1 小时前
Agent演化史(十):2027年它会消失吗?(终章)
python·agent·智能体·大模型应用
琳少_1 小时前
java课堂笔记
java·开发语言·数据库·笔记
专注仿真2 小时前
仿真智能体训练:强化学习中的奖励塑形算法与实战
python·强化学习·afsim智能体·仿真训练·afsim智能体训练
3095566662 小时前
股票接口日期参数为什么不起作用:五组请求对照与逐字节比对排查笔记
服务器·数据库·笔记·python·量化·api接口·股票接口
Brain 博览记3 小时前
脑机接口研究丨FPGA 5 毫秒完成运动想象分类
开发语言·论文阅读·人工智能·机器学习·fpga开发·分类·数据挖掘
JonaHu3 小时前
GPT Image 2.6 改图交付防串版:文件指纹、批准清单与下载验收
图像处理·人工智能·python
Brain 博览记4 小时前
脑网络AI丨给图神经网络装上“信息高速路”
开发语言·网络·论文阅读·人工智能·深度学习·神经网络·机器学习
鱼很腾apoc4 小时前
【Redis】第2期 详解Redis五大数据结构:命令+C++实现、渐进式遍历与数据库管理
linux·服务器·c语言·数据结构·c++·redis·学习
Java码农也是农4 小时前
Rust、Java、Python、C++ 有什么区别?从 CPU 执行到内存管理
java·python·rust