Why use cocos2d::Vector instead of std::vector

I was looking at the documentation, and the cocos2d::Vector is the same as the std::vector.
So why should we use it instead of the std::vector ?
can I not use C++ libraries with cocos2d-x?

You can use std::vector if you like. I use a mixture of both in my code.

cocos2d::Vector has the limitation that the objects added to it must have cocos2d::Ref as a base class. The benefit comes with memory management. When you add an object to a cocos2d::Vector, it retains the object. When you remove an object from a cocos2d::Vector, it releases the object. This helps to ensure that the objects in the cocos2d::Vector are valid objects (unless you start manually releasing things like crazy for no reason)

1 Like

You can of course use C++ libraries with Cocos2d-x. It is C++ itself.

I prefer to use std::vector myself.

just came across this topic, and I’m curious if cocso2d even uses their own STL to prevent memory fragmentation like others engines to by avoiding STL.

Even though cocos2d::Vector exists i’ve seen the STL all over the place in the cocos2d engine.

Just curious, for cocso2d i’m using the std lib with its STL myself, because on mobile with 2D i kinda don’t care about the fragmentation, and overhead since mobile devices nearly have the same spec as consoles do lol.

cocos2d::Vector is just std::vector underneath (std::vector<T> _data;), wrapped to handle retain/release requirements and thus prevents certain low-level direct access to the underlying container.

The engine doesn’t really have its own and uses the c++ STL internally (clang’s as of latest version for ios/android), except for where high performance hash table where required. The 3rd party libraries, especially Box2D and others probably have their own STL due to high performance needs. The engine doesn’t really support custom allocation strategies: no pooling, or custom allocators, as far as I know.

There was a custom allocator system being worked on, but it’s half-baked and is abandoned now.

Honestly, the standard c++ STL is decently performant, if a bit bloated (due to being generic). AAA games and physics simulation has a need for higher-performance and they use custom containers with custom allocation strategies.

and I don’t know if this will be brought back as part of v4 at this time. I hope that it will be.