所以我有这个结构:
struct foo{
DWORD fooB;
char *fooA;
}
并且我有一个变量DWORD bar;
,那么如何确定bar
是否与结构中的任何fooB
相匹配?
编辑:我的代码(当前)
#include <algorithm> // for. std::find
using namesapce std;
struct foo{
DWORD fooB;
char *fooA;
// .... Use this
}
vector <DWORD> foo;
if ( std::find(vector.begin(),
vector.end(), pIdToFind) !=
vector.end() )
// We found the item in the list, so let's just continue
else
// We haven't found it,
答案 0 :(得分:3)
您只需提供一个比较运算符即可将DWORD
与foo
进行比较:
#include <vector>
#include <algorithm>
#include <windows.h>
struct foo {
DWORD fooB;
char *fooA;
};
bool operator==(DWORD lhs, foo const &rhs)
{
return lhs == rhs.fooB;
}
int main()
{
foo needle{ 42, nullptr };
vector<DWORD> haystack;
if (std::find(haystack.begin(), haystack.end(), needle) != haystack.end())
{
// We found the item in the list, so let's just continue
}
else
{
// not found
}
}