diff --git a/cocos/base/CCVector.h b/cocos/base/CCVector.h index 2a12c66d76..2314e325ba 100644 --- a/cocos/base/CCVector.h +++ b/cocos/base/CCVector.h @@ -25,10 +25,12 @@ THE SOFTWARE. #ifndef __CCVECTOR_H__ #define __CCVECTOR_H__ -#include -#include // std::for_each #include "ccMacros.h" +#include +#include +#include // std::for_each + NS_CC_BEGIN template @@ -299,9 +301,19 @@ public: _data.shrink_to_fit(); } - void forEach(std::function callback) + void forEach(const std::function& callback) { - if (size() <= 0) + if (empty()) + return; + + std::for_each(_data.cbegin(), _data.cend(), [&callback](const T& obj){ + callback(obj); + }); + } + + void forEach(const std::function& callback) const + { + if (empty()) return; std::for_each(_data.cbegin(), _data.cend(), [&callback](const T& obj){ @@ -309,9 +321,19 @@ public: }); } - void forEachReverse(std::function callback) + void forEachReverse(const std::function& callback) { - if (size() <= 0) + if (empty()) + return; + + std::for_each(_data.crbegin(), _data.crend(), [&callback](const T& obj){ + callback(obj); + }); + } + + void forEachReverse(const std::function& callback) const + { + if (empty()) return; std::for_each(_data.crbegin(), _data.crend(), [&callback](const T& obj){