From 6f13a111ae6e3d96b4294a6f0f6af97a1c47b0db Mon Sep 17 00:00:00 2001 From: James Chen Date: Sat, 7 Dec 2013 14:18:42 +0800 Subject: [PATCH] issue #2790: Adds Vector::sort method, adds Vector::removeObject(object, toRemoved). --- cocos/base/CCVector.h | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/cocos/base/CCVector.h b/cocos/base/CCVector.h index 2314e325ba..3ea40ccc8a 100644 --- a/cocos/base/CCVector.h +++ b/cocos/base/CCVector.h @@ -230,13 +230,14 @@ public: } /** Remove a certain object */ - void removeObject(T object) + void removeObject(T object, bool toRelease = true) { CCASSERT(object != nullptr, "The object should not be nullptr"); auto iter = std::find(_data.begin(), _data.end(), object); if (iter != _data.end()) _data.erase(iter); - object->release(); + if (toRelease) + object->release(); } /** Removes an element with a certain index */ @@ -341,6 +342,16 @@ public: }); } + void sort(const std::function& callback) + { + if (empty()) + return; + + std::sort(_data.begin(), _data.end(), [&callback](T a, T b) -> bool{ + return callback(a, b); + }); + } + // ------------------------------------------ // Iterators // ------------------------------------------