In C++, is there an array class that works similar to the Python list class? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

In C++, is there an array class that works similar to the Python list class?

I want to create a list of data items that is managed through member functions such as below. I know I can make an array. But the member functions would need to be written by me I think. Is there a list/array class built-in to C++ that accomplishes something like this? Something like this would manage memory allocation, etc? myarray::add(void * data); myarray::remove(void * data); myarray::find(void * data);

20th Apr 2021, 12:53 AM
Jerry Hobby
Jerry Hobby - avatar
4 Answers
+ 4
Do you mean STL containers? Like std::list/array/vector/map/set etc?
20th Apr 2021, 1:42 AM
Michal Doruch
+ 6
std::vector -> Python list (dynamic array) std::map -> Python dictionary std::set -> Python set std::tuple -> Python tuple See the containers library on cppereference whenever you're wondering if a specific data structure is there in the C++ STL https://en.cppreference.com/w/cpp/container
20th Apr 2021, 2:08 AM
XXX
XXX - avatar
+ 4
Mirielle Python lists and std::vector are the same data structure (dynamic arrays). So std::vector is as close as it gets to Python lists. No data structure will allow you to have different types in C++ as you can do in Python (except for std::tuple, but that's fixed size). That's the basic difference between Python and C++, that types matter in C++ and they don't in Python. So the point that Python lists can store any type while std::vector can't, doesn't really stand in my opinion. The best you can do is use std::variant, but then also you will be limited to the types you passed as template arguments. It will also be a great waste of memory as std::variant<char, double> occupies 16 bytes of space even if you have a 1 byte char stored in it.
20th Apr 2021, 4:57 AM
XXX
XXX - avatar
+ 1
Michał Doruch That’s what i was looking for. Thanks.
20th Apr 2021, 1:48 AM
Jerry Hobby
Jerry Hobby - avatar