Python list equivalent in C++?

前端 未结 5 984

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 17:48

    There is no real equivalent, and it would be extremely difficult to provide one. Python and C++ are radically different languages, and providing one really wouldn't make much sense in the context of C++. The most important differences are that everything in Python is dynamically allocated, and is an "object", and that Python uses duck typing.

    FWIW: one very early library (before templates) in C++ did offer containers of Object*, with derived classes to box int, double, etc. Actual experience showed very quickly that it wasn't a good idea. (And I'm curious: does any one else remember it? And particularly, exactly what it was called---something with NHS in it, but I can't remember more.)

    0 讨论(0)
  • 2021-02-05 17:53

    I am working on a wrapper for std::vector that makes it more like Python's lists named pylistpp. The API is just like Python. Example:

    #include <list.hpp>
    #include <iostream>
    
    int main()
    {
        list<int> mylist;
        mylist.append(5);
        mylist.append(7);
        int count = mylist.count(5);
        std::cout << count << std::endl;
        std::cout << mylist.pop(0) << std::endl;
        std::cout << mylist.index(7);
        return 0;
    }
    
    0 讨论(0)
  • 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 <vector>
    #include <string>
    #include <iostream>
    
    int main()
    {
        std::vector<boost::any> myList;
    
        myList.push_back(std::string("Hello"));
        myList.push_back(10);
        myList.push_back(std::string("World"));
    
        std::string any1 = boost::any_cast<std::string> (myList[0]);
        int any2 = boost::any_cast<int> (myList[1]);
        std::string any3 = boost::any_cast<std::string> (myList[2]);
    
        std::cout<<any1<<" "<<any2<<" "<<any3<<std::endl;
    
        return 0;
    }
    
    0 讨论(0)
  • 2021-02-05 18:10

    std::vectors, std::lists, and arrays (or std::arrays) all have features similar to Python lists. Which data structure you want to choose depends on your requirements.

    0 讨论(0)
  • 2021-02-05 18:12

    Actually no C++ container is equivalent to Python's list, which is partially a result of the very different object models of C++ and Python. In particular, the suggested and upvoted std::list is IMHO not even close to Python's list type, a I'd rather suggest std::vector or maybe std::deque. That said, it isn't clear what exactly it is that you want and how to "build it" strongly depends on what exactly "it" is, i.e. what you expect from the container.

    I'd suggest you take a look at the C++ containers std::vector, std::deque and std::list to get an overview. Then look at things like Boost.Any and Boost.Variant that you can combine with them, maybe also one of the smart pointers and Boost.Optional. Finally, check out Boost.Container and Boost.Intrusive. If the unlikely case that none of these provide a suitable approximation, you need to provide a better explanation of what your actual goals are.

    0 讨论(0)
提交回复
热议问题