Python list equivalent in C++?

前端 未结 5 992

Could you please tell me what is the closest data type in C++ to python list? If there is nothing similar, how would you build it in C++?

5条回答
  •  故里飘歌
    2021-02-05 18:04

    Maybe storing boost::any in a std::vector? http://www.boost.org/doc/libs/1_54_0/doc/html/boost/any.html

    Here is a simple working example. See James comments below too.

    #include "../boost_1_54_0/boost/any.hpp"
    #include 
    #include 
    #include 
    
    int main()
    {
        std::vector myList;
    
        myList.push_back(std::string("Hello"));
        myList.push_back(10);
        myList.push_back(std::string("World"));
    
        std::string any1 = boost::any_cast (myList[0]);
        int any2 = boost::any_cast (myList[1]);
        std::string any3 = boost::any_cast (myList[2]);
    
        std::cout<

提交回复
热议问题