http://topic.csdn.net/t/20061024/01/5103918.html


#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
struct Npos{
int x;
int y;
int f;
};
typedef Npos npos;
void main(){
npos a;
npos b;
b.x=5;
b.y=9;
b.f=3;
a.x=2;
a.y=6;
a.f=2;
std::vector<npos> sai;
sai.push_back(a);
sai.push_back(b);
std::vector<npos>::const_iterator ss;
ss=std::find(sai.begin(),sai.end(),2);
cout<<ss<<endl;
}
里面那个查找有错误
c:\sai\include\algorithm(43) : error C2676: binary '==' : 'struct Npos' does not define this operator or a conversion to a type acceptable to the predefined operator
C:\Documents and Settings\Administrator\abs.cpp(25) : see reference to function template instantiation 'struct Npos *__cdecl std::find(struct Npos *,struct Npos *,const int &)' being compiled
执行 cl.exe 时出错.
哪位高手可以指点一下,还有就是写一下可以实现的代码!万分感谢


没有重载“==”运算符,编译器不懂怎么判断一个npos对象和一个int对象在什么情况下是“相等的”……
下面是一个可用的例子
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
struct Npos{
int x;
int y;
int f;
bool operator==(const Npos npos)
{
return x==npos.x && y==npos.y && f==npos.f;
}
Npos(){}
Npos(int a,int b,int c)
{
x=a;y=b;f=c;
}
};
typedef Npos npos;
void main(){
npos a;
npos b;
b.x=5;
b.y=9;
b.f=3;
a.x=2;
a.y=6;
a.f=2;
std::vector<npos> sai;
sai.push_back(a);
sai.push_back(b);
std::vector<npos>::const_iterator ss;
ss=std::find(sai.begin(),sai.end(),Npos(2,9,2));
if (ss!=sai.end())
cout<<(*ss).x<<endl<< (*ss).y<<endl <<(*ss).f <<endl;
else
cout<<"can not find!"<<endl;
}
楼主也可以在npos中自定义一个
bool operator==(const int i)
这样的话,调用ss=std::find(sai.begin(),sai.end(),2);之类的语句就不会出错了。