C++迭代集合
我最近更改了一些代码以使用集合而不是向量:
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*>]()'
I recently changed some code to use a set instead of a vector:
std::set<b2Body *>toDestroy;
//std::vector<b2Body *>toDestroy;
But now I'm not sure how to iterate the set to find objects. This is what I had:
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);
}
What is the equivalent now that toDestroy is a set? Coming from Objective-C so I'm just learning best practices in C++.
EDIT: adding the error message I get:
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*>]()'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要将迭代器声明为
set
迭代器:更改
为
You need to declare your iterator to be a
set
iterator:Change
to
使用 C++11 你可以简单地写:
With C++11 you could simply write:
通过集合的迭代将像向量存在时一样工作,因此无需更改代码。
我会注意 DestroyBody 中发生的情况(调用是否从向量或集合中删除元素,从而使迭代器无效?)
此外,向量、集合或列表的使用取决于用法:
我认为这里最合适的容器是列表(并且迭代代码仍然不需要更改)
The iteration through the set will work as when the vector is there, so there is no need to change the code.
I would pay attention to what happen in DestroyBody (does the call remove the element from the vector or set, invalidating the iterator?)
Also the usage of vector, set or list depend on the usage:
I think that the most appropriate container here is the list (and still the iteration code wouldn't need to be changed)
很多时候值得对模板化容器类型进行类型定义(特别是在类内部使用时)。
那么您的其他代码就不需要修改:
如果您可以将 typedef 设为私有,那么您就知道实现细节不会逃逸该类。如果您需要公开 typedef,那么您就知道您正在泄漏实现细节,并且需要了解原因以及是否可以加强您的设计。
A lot of time it is worth type-defing templated container types (especially when used inside a class).
Then your other code would have not needed modifying:
If you can make the typedef private then you know that implementation details are not escaping the class. If you need to make the typedef public then you know you are leaking implementation details and need to understand why and if you can tighten your design.
从 C++11 开始,当迭代容器时,应该使用“auto”来声明迭代器。
在你的情况下,“for”行可以写成这样:
for(auto pos2 = toDestroy.begin(); pos2 != toDestroy.end(); ++pos2)
Since C++11, when iterating over a container, you should use "auto" to declare an iterator.
In your case, the "for" line can be written like this:
for(auto pos2 = toDestroy.begin(); pos2 != toDestroy.end(); ++pos2)