我已经定义了一个结构坐标为如何在矢量中查找元素?
struct Coord {
int x;
int y;
int z;
};
重载运算符!=为坐标
bool Coord::operator !=(Coord& crd)const {
if(this->x != crd.x)
{
if(this->y != crd.y)
{
if(this->z != crd.z)
return true;
else
return false;
}
else
return false;
return true;
}
else
return false;
}
然后初始化向量变量作为
vector<Coord> vcoord;
现在,我使用下面的代码获取具有特定Coord对象的向量索引
int Index::getVIndex(Coord crd) {
vector<Coord>::iterator it;
int indx;
it = vcoord.begin();
while(it != vcoord.end() && (*it) != crd)
++it;
indx = distance(vcoord.begin(),it);
cerr << (*it).x << " " << (*it).y << " " << indx << endl;
return indx;
}
但是,indx的值总是0.请帮助获得正确的结果。
它是坐标还是坐标? –
这是否甚至编译? – juanchopanza
你在'vcoord'中输入了什么值,你传递给'getVIndex'什么值? 'Coord'定义了'operator!='是如何定义的?更好的是,你能发布一个小而完整的程序来证明问题吗? –