Python list equivalent in C++?

前端 未结 5 990

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.)

提交回复
热议问题