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 Answers
If you're looking for standard one-dimensional data structures idiomatic to C++, std::vector
s, std::list
s, and arrays (or std::array
s) all have features similar to Python lists. Which of those data structure you want to choose depends on your requirements. std::vector
is a very reasonable structure to default to if you're just looking to store a collection of items.
These data structures all require that each element is of the same type. Generally in C++ that is what you want. However, if you're specifically looking to store a variety of types in the same structure, there are a few options:
- You can store
void
pointers. You will, however, need some way to figure out the type of each element and cast the pointer to the appropriate type to use each element. - If you have a specific set of types you wish to store, you can declare a
union
(which you'd generally wrap in astruct
along with anenum
to indicate the type being stored).
-
2Python lists can store elements of different types. docs.python.org/release/1.5.1p1/tut/lists.html Jul 8, 2013 at 14:10
-
2@Korchkidu True, but that wasn't something that the asker specifically asked for. And doing that in C++, especially as a beginner, is asking for trouble. Unless gen edits the question to say that they're specifically asking for something that can store any type, then I'm going to interpret the question as asking for a random-access one-dimensional data structure. Jul 8, 2013 at 14:16
-
2@EricFinn He didn't specifically ask for anything. Presumably, he knows about
std::vector
(who doesn't), and that's not close enough. Why it's not close enough is another question/ Jul 8, 2013 at 14:51 -
@Eric Why would you assume that? You clearly cut down the functionality.– luk32Jul 26, 2013 at 9:15
-
@luk32 Because it made sense to me that the asker was asking for a way to store a list of things that's idiomatic to C++. I could have answered the question exactly as written and linked to the cpython list implementation, but I didn't think that would be as helpful to the asker and answer the question gen meant to ask. Jul 26, 2013 at 12:12
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;
}
-
1Actually, storing a
std::shared_ptr<Object>
, and then creating (template?) wrapper classes deriving fromObject
would come closer. Python list elements have reference semantics;boost::any
value semantics. Jul 8, 2013 at 14:34 -
@JamesKanze: Yes. indeed. Thanks for the information. Not sure how far he wants to go in Python direction though;) Jul 8, 2013 at 14:45
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.
-
Don't forget that you can write something like
l2 = l1[3:20:2]
to get every other element in the range[3...20)
. And that negative indexes index from the far end. Jul 8, 2013 at 14:33 -
Python's slices are very powerful indeed, you can even use them to insert or delete elements. Musing about what a container can or can't do is futile though, the OP needs to clarify what exactly they need. Jul 8, 2013 at 16:19
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.)
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;
}
std::tuple
then.boost::any
would be closer, except that Python elements have reference semantics, which would bestd::shared_ptr
. Except that Python uses duck typing, so you'd have to provide every possible function inObject
, with a default implementation which throws. Not to mention that Python supports negative indexes, simple and extended slicing, and I don't know what all else.