如果我有一个对象矢量矢量,我如何检查A * myA是否在该矢量内?
答案 0 :(得分:5)
试试这个......
#include <algorithm>
bool in_vector(const std::vector<A*>& vec, const A* myA)
{
std::vector<A*>::const_iterator end = vec.end();
return std::find(vec.begin(), end, myA) != end;
}
答案 1 :(得分:0)
这可能有效,也可能无效:
vector<int> v;
. . .
for (int i=0; i<v.size(); i++) {
//---do a compare of v[i] with your object?
cout << v[i] << endl;
}
答案 2 :(得分:0)
也许是这样的:
std::vector<A*> v;
for (std::vector<A*>::iterator it = v.begin(); it != v.end(); ++it) {
// Do comparison with *it to your myA...
}
答案 3 :(得分:0)
对于我认为更接近你想做的事情,这是对wowus'答案的扩展:
#include <algorithm>
// let's call it simply TVectorVectorA and TVectorA
typedef std::vector<std::vector<A*> > TVectorVectorA;
typedef TVectorVectorA::value_type TVectorA;
bool in_vector(const std::vector<std::vector<A*> >& vec, const A* myA)
{
TVectorVectorA::const_iterator vend = vec.end();
// iterate through all 'top-level' vectors
for (TVectorVectorA::const_iterator it = vec.begin(); vend != it; ++it)
{
// check if the current TVector element contains myA
if (std::find((*it).begin(), (*it).end(), myA) != end)
{
return true;
}
}
return false;
}
在提出相关问题时,提供相关的代码示例(即您正在使用的数据类型的声明)总是很好。通过这种方式,其他人可以更轻松地了解您的目标。