c – 向量,迭代器和std :: find
发布时间:2020-12-16 06:47:19 所属栏目:百科 来源:网络整理
导读:有没有办法在不同的向量中使用不同类型的迭代器?或者,是否有一个函数将向量中的元素的位置作为整数返回? std::vectorDWORD::iterator it; // Iterator// monsterQueue is a DWORD vectorit = std::find(bot.monsterQueue.begin(),bot.monsterQueue.end(),o
有没有办法在不同的向量中使用不同类型的迭代器?或者,是否有一个函数将向量中的元素的位置作为整数返回?
std::vector<DWORD>::iterator it; // Iterator // monsterQueue is a <DWORD> vector it = std::find(bot.monsterQueue.begin(),bot.monsterQueue.end(),object); // Check do we have the object in the queue if(it != bot.monsterQueue.end()) // If we do have it { bot.monsterDists.at(it) = mobDist; // monsterDists is <int> vector bot.monsterCoordX.at(it) = PosX; // monsterCoordX is <int> vector bot.monsterCoordY.at(it) = PosY; // monsterCoordY is <int> vector too } 这是一些示例代码,有没有人有任何指针? 解决方法
尝试
std::vector<DWORD>::iterator it; // Iterator // monsterQueue is a <DWORD> vector it = std::find(bot.monsterQueue.begin(),object); // Check do we have the object in the queue if(it != bot.monsterQueue.end()) // If we do have it { size_t idx = it – bot.monsterQueue.begin() bot.monsterDists.at(idx) = mobDist; // monsterDists is <int> vector bot.monsterCoordX.at(idx) = PosX; // monsterCoordX is <int> vector bot.monsterCoordY.at(idx) = PosY; // monsterCoordY is <int> vector too } 也可能更好的想法是创建一个包含4个成员’monster’,monsterDist和coordinateX以及coordinateY的结构,并将结构对象存储在向量中. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |