C++ 向量、结构和 std::find
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/589985/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Vectors, structs and std::find
提问by Ahmed
Again me with vectors. I hope I'm not too annoying. I have a struct like this :
再次我与向量。我希望我不会太烦人。我有一个这样的结构:
struct monster
{
DWORD id;
int x;
int y;
int distance;
int HP;
};
So I created a vector :
所以我创建了一个向量:
std::vector<monster> monsters;
But now I don't know how to search through the vector. I want to find an ID of the monster inside the vector.
但现在我不知道如何搜索向量。我想在向量中找到怪物的 ID。
DWORD monster = 0xFFFAAA;
it = std::find(bot.monsters.begin(), bot.monsters.end(), currentMonster);
But obviously it doesn't work. I want to iterate only through the .id element of the struct, and I don't know how to do that. Help is greatly appreciated. Thanks !
但显然它不起作用。我只想遍历结构的 .id 元素,但我不知道该怎么做。非常感谢帮助。谢谢 !
回答by Johannes Schaub - litb
it = std::find_if(bot.monsters.begin(), bot.monsters.end(),
boost::bind(&monster::id, _1) == currentMonster);
Or write your own function object if you don't have boost. Would look like this
或者,如果您没有 boost,则编写自己的函数对象。看起来像这样
struct find_id : std::unary_function<monster, bool> {
DWORD id;
find_id(DWORD id):id(id) { }
bool operator()(monster const& m) const {
return m.id == id;
}
};
it = std::find_if(bot.monsters.begin(), bot.monsters.end(),
find_id(currentMonster));
回答by Ahmed
how about:
怎么样:
std::find_if(monsters.begin(),
monsters.end(),
[&cm = currentMonster]
(const monster& m) -> bool { return cm == m; });
回答by Evgeny Lazin
You need to write your own search predicate:
您需要编写自己的搜索谓词:
struct find_monster
{
DWORD id;
find_monster(DWORD id) : id(id) {}
bool operator () ( const monster& m ) const
{
return m.id == id;
}
};
it = std::find_if( monsters.begin(), monsters.end(), find_monster(monsterID));
回答by dirkgently
Take a look at the std::find
template, the third parameter especially:
看一下std::find
模板,特别是第三个参数:
template<class InputIterator, class EqualityComparable>
InputIterator find(InputIterator first, InputIterator last,
const EqualityComparable& value);
What is this EqualityComparable? Again from the documentation:
这个 EqualityComparable 是什么?再次来自文档:
A type is EqualityComparable if objects of that type can be
compared for equality using operator==, and if operator== is
an equivalence relation.
Now, your type monster needs to define such an operator. If you don't the compiler generates one for you (as also the default ctor and the dtor) which does a memcmp
sort of thing which doesn't work in your case. So, to use std::find
first define a comparator function/functor that the algorithm can use to match your currentMonster
i.e. something along the lines of:
现在,您的类型怪物需要定义这样的运算符。如果您不这样做,编译器会为您生成一个(以及默认的 ctor 和 dtor),它会执行memcmp
某种在您的情况下不起作用的事情。所以,std::find
首先定义一个比较器函数/函子,算法可以使用它来匹配你的currentMonster
ie 东西:
struct monster {
// members
bool operator==(const monster& l, const monster& r) const
{
return l.id == r.id;
}
};
回答by pm100
or put the monsters in a map instead of a vector
或者将怪物放在地图而不是矢量中
or if they must be in a vector create an index map ie map of ID to vector index
或者如果它们必须在向量中,则创建一个索引映射,即 ID 到向量索引的映射
回答by Semjon M?ssinger
This is a complete sample based on the answer of Johannes Schaub (boost version).
这是一个基于 Johannes Schaub (boost version) 答案的完整示例。
#include <algorithm>
#include <boost/bind.hpp>
struct monster
{
DWORD id;
int x;
int y;
int distance;
int HP;
};
int main ()
{
std::vector<monster> monsters;
monster newMonster;
newMonster.id = 1;
newMonster.x = 10;
monsters.push_back ( newMonster );
newMonster.id = 2;
newMonster.x = 20;
monsters.push_back ( newMonster );
newMonster.id = 2;
newMonster.x = 30;
monsters.push_back ( newMonster );
DWORD monsterId = 2;
std::vector< monster >::iterator it = std::find_if ( monsters.begin (), monsters.end (),
boost::bind ( &monster::id, _1 ) == monsterId );
return 0;
}
回答by Aditya Sharma
You can write a function as below:
您可以编写如下函数:
monster* findMonster(DWORD currentMonster) {
for (auto it = bot.monsters.begin(); it != bot.monsters.end(); it++) {
if (it->id == currentMonster) {
return &(*it);
}
}
return NULL;
}
It returns a pointer to the stored node if it's found in the vector, otherwise returns NULL.
如果在向量中找到它,它返回一个指向存储节点的指针,否则返回 NULL。
Please note that return it;
won't work directly.
请注意,return it;
这不会直接工作。