如何使用结构体的向量数组?

How to work with a vector array of struct?

本文关键字:数组 向量 何使用 结构体      更新时间:2023-10-16

所以我有这个结构:

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, 

您可以简单地提供一个比较运算符来比较DWORDs 和foos:

#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
}
}