简体   繁体   English

C ++在向量中找到某些东西

[英]C++ finding something in vector

I have an vector like this: 我有一个像这样的向量:

struct RIFT_MONSTER_SPAWN
{
    DWORD dwWorldId;
    D3DXVECTOR3 vPos;
};

vector<RIFT_MONSTER_SPAWN> vecMSpawn;

As you can see it will hold 2 values dwWorldId and D3DXVECTOR3 vPos; 如您所见,它将包含2个值dwWorldIdD3DXVECTOR3 vPos; vPos will hold x,y,z value. vPos将保存x,y,z值。

Now what I want to do is looping true the vector and if the worldId matches the worldId that is beeing passed. 现在我想做的是真正的循环载体和如果worldId的匹配worldId是beeing通过。 It should return the vPos that is releated to the worldId . 它应返回与vPos相关的worldId

If have use std::find find_all and count . 如果使用std::find find_allcount But it always returns the error binary == no operator found which takes a left hand operator of type 但是它总是返回错误二进制==找不到操作符,该操作符需要使用类型为左的操作符

So I am an bit stuck on this. 所以我对此有些困惑。 Any help would be appreciated. 任何帮助,将不胜感激。

With kind regards. 亲切的问候。

Here is the code that is giving me problems 这是给我问题的代码

void CRiftMatch::GetMoverSpawnPoints(dwWorldId)
{
    std::vector<RIFT_MONSTER_SPAWN> vecSpawn = CRiftMng::GetInstance()->m_vecMSpawnPoint;
    std::vector<RIFT_MONSTER_SPAWN>::iterator it = std::find(vecSpawn.begin(), vecSpawn.end(), dwWorldId);
    OUTPUTDEBUGSTRING("\n GetMoverSpawn %d", *it);
}

m_vecMSpawnPoint is defined in .h file as m.vecMSpawnPoint在.h文件中定义为

vector<RIFT_MONSTER_SPAWN> m_vecMSpawnPoint;

Also to fill it i am using this code 还要填写我正在使用此代码

while (Lua.TableLoop(-2))
        {
            RIFT_MONSTER_SPAWN rSpawnPoint;
            rSpawnPoint.dwWorldId = static_cast<int>(CScript::GetDefineNum(Lua.GetFieldToString(-1, "dwWorldId")));
            rSpawnPoint.vPos.x = static_cast<float>(Lua.GetFieldToNumber(-1, "x"));
            rSpawnPoint.vPos.y = static_cast<float>(Lua.GetFieldToNumber(-1, "y"));
            rSpawnPoint.vPos.z = static_cast<float>(Lua.GetFieldToNumber(-1, "z"));

            m_vecMSpawnPoint.push_back(rSpawnPoint);
            Lua.Pop(1);
        }

You have to modify your struct to be able compare values during find: 您必须修改结构才能在查找过程中比较值:

struct RIFT_MONSTER_SPAWN
{
    DWORD dwWorldId;
    D3DXVECTOR3 vPos;

    bool operator () ( const RIFT_MONSTER_SPAWN & m ) const
    {
        return m.dwWorldId == dwWorldId;
    }
};

RIFT_MONSTER_SPAWN monsterToFind;
monsterToFind.dwWorldId = dwWorldId;

it = std::find_if( vecSpawn.begin(), vecSpawn.end(), monsterToFind);

Maybe just a type, but in your code, you have 也许只是一种类型,但是在您的代码中,

void CRiftMatch::GetMoverSpawnPoints(dwWorldId)

but it should be 但这应该是

void CRiftMatch::GetMoverSpawnPoints(DWORD dwWorldId)

You could pass a predicate (which can be a lambda) to std::find_if so code 您可以将谓词(可以是lambda)传递给std::find_if因此代码

  auto it = std::find_if(vecSpawn.begin(), vecSpawn.end(),
                         [&] (const struct RIFT_MONSTER_SPAWN& sp) 
                            {return sp.dxWorldId == dwWorldIt;});

But in such a case I would simply use a for loop (because I find that more readable): 但是在这种情况下,我只会使用for循环(因为我发现它更具可读性):

  int ix=0;
  for (auto&sp : vecSpawn) {
     if (sp.dxWorldId == dwWorldIt) 
        return vecSpawn.begin() + ix;
     ix++;
  }

With range/v3 , you may simply do 使用range / v3 ,您可以简单地执行

auto it = ranges::find(vecSpawn, dwWorldIt, &RIFT_MONSTER_SPAWN::dxWorldId);

else you have to use more verbose 否则,您必须使用更多详细信息

auto it = std::find_if(vecSpawn.begin(), vecSpawn.end(),
                       [&](const RIFT_MONSTER_SPAWN& e) {
                           return e.dxWorldId == dwWorldIt;
                       });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM