【发布时间】:2012-10-25 10:43:42
【问题描述】:
在某些类中,我有一个 hash_set 指针:
std::hash_set<GameObject*> gameObjects;
我希望遍历所有这些并调用更新。使用迭代器会是这样的:
for(std::hash_set<GameObject*>::iterator it = gameObjects.begin(), it_end = gameObjects.end(); it != it_end; ++it)
{
(*it)->update();
}
代码太多了;我想使用std::for_each。
如果我只有hash_set<GameObject>,这将是这样的:
std::for_each(gameObjects.begin(), gameObjects.end(), std::mem_fun_ref(&GameObject::update));
或者,如果更新需要一个参数,
std::for_each(gameObjects.begin(), gameObject.end(), std::bind2nd(std::mem_fun_ref(&GameObject::update), deltaTime));
这两个表达式如何查找指针的hash_set?
我正在使用 MS VisualStudio 2010。
【问题讨论】:
-
您使用的是哪个编译器?有多少 C++11 可用?
标签: c++ pointers foreach hashset