我最近将一些代码从vector改为了set:
std::set<b2Body *>toDestroy;
//std::vector<b2Body *>toDestroy;
但现在我不确定如何迭代集合以查找对象。这是我的代码:
std::vector<b2Body *>::iterator pos2;
for(pos2 = toDestroy.begin(); pos2 != toDestroy.end(); ++pos2) {
b2Body *body = *pos2;
if (body->GetUserData() != NULL) {
CCSprite *sprite = (CCSprite *) body->GetUserData();
[self removeChild:sprite cleanup:YES];
}
_world->DestroyBody(body);
}
现在toDestroy是一个集合,那么现在的等价物是什么?我来自Objective-C,正在学习C++的最佳实践。
编辑:添加我收到的错误信息:
error: no match for 'operator=' in 'pos2 = toDestroy. std::set<_Key, _Compare, _Alloc>::begin [with _Key = b2Body*, _Compare = std::less<b2Body*>, _Alloc = std::allocator<b2Body*>]()'