关于STL的vector查找问题 (解决NrNsNtNz_InTitle中 由 文本体中发现的标题未被正确识别出的NrNsNtNz)...

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

 

 

ContractedBlock.gif ExpandedBlockStart.gif Code
#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   时出错.   
    
  哪位高手可以指点一下,还有就是写一下可以实现的代码
!万分感谢   
  

 

 

 

ContractedBlock.gif ExpandedBlockStart.gif Code
没有重载“==”运算符,编译器不懂怎么判断一个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);之类的语句就不会出错了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值