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;
} 
相关推荐
泽安AI研习社11 分钟前
Cursor用户集体倒戈 !这14招让你榨干Claude Code【建议收藏】
人工智能·python
超浪的晨14 分钟前
Java List 集合详解:从基础到实战,掌握 Java 列表操作全貌
java·开发语言·后端·学习·个人开发
盛夏绽放17 分钟前
Excel导出实战:从入门到精通 - 构建专业级数据报表的完整指南
开发语言·javascript·excel·有问必答
我命由我1234517 分钟前
嵌入式单片机开发 - HAL 库 STM32F1 外设的时钟使能(时钟使能宏、时钟禁用宏)
c语言·c++·stm32·单片机·嵌入式硬件·嵌入式·嵌入式软件
超浪的晨20 分钟前
Java Set 集合详解:从基础语法到实战应用,彻底掌握去重与唯一性集合
java·开发语言·后端·学习·个人开发
workflower35 分钟前
活动图描述场景
开发语言·软件工程·需求分析·软件需求·敏捷流程
梦想的初衷~37 分钟前
基于现代R语言【Tidyverse、Tidymodel】的机器学习方法
开发语言·机器学习·r语言
liliangcsdn37 分钟前
mac mlx大模型框架的安装和使用
java·前端·人工智能·python·macos
香蕉可乐荷包蛋41 分钟前
Python学习之路(十三)-常用函数的使用,及优化
开发语言·python·学习
chian-ocean44 分钟前
零基础入门:用C++从零实现TCP Socket网络小工具
网络·c++·tcp/ip