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;
} 
相关推荐
咩咦2 分钟前
C++学习笔记22:前置后置 ++/-- 和日期减日期
c++·学习笔记·运算符重载·日期类·前置++·后置++·日期减日期
计算机安禾6 分钟前
【c++面向对象编程】第40篇:单例模式(Singleton)的多种C++实现
开发语言·c++·单例模式
new_dev14 分钟前
Python实现Android自动化打包工具:加固、签名、多渠道一键完成
android·python·自动化
_日拱一卒22 分钟前
LeetCode:114二叉树展开为链表
java·开发语言·算法
天天进步201524 分钟前
从零打造 Python 全栈项目:智能教学辅助系统
开发语言·人工智能·python
一个不知名程序员www34 分钟前
算法学习入门---算法题DAY1
c++·算法
kkeeper~41 分钟前
0基础C语言积跬步之内存函数
c语言·开发语言
吃好睡好便好42 分钟前
在Matlab中绘制杆状图
开发语言·学习·算法·matlab·信息可视化
带带弟弟学爬虫__42 分钟前
dyAPP数据采集-个人主页、发布、搜索、评论
服务器·python·算法·flutter·java-ee·django
还是鼠鼠1 小时前
AI掘金头条新闻系统 (Toutiao News)-相关推荐
后端·python·mysql·fastapi·web