使用STL的find函数遇到的问题

先查了下资料 http://www.cplusplus.com/reference/algorithm/find/


Example

/ find example
#include <iostream>

#include <algorithm>

#include <vector>

using namespace std;

int main ()
 {
   int myints[] = { 10, 20, 30 ,40 };
   int * p;

   // pointer to array element:
   p = find(myints,myints+4,30);
   ++p;
   cout << "The element following 30 is " << *p << endl;

   vector<int> myvector (myints,myints+4);
   vector<int>::iterator it;

  // iterator to vector element:
   it = find (myvector.begin(), myvector.end(), 30);
   ++it;
   cout << "The element following 30 is " << *it << endl;

   return 0;
}



Output:
The element following 30 is 40
The element following 30 is 40


但是自己的project 编译完了报错Error 1 error C2678: binary '==' : no operator found which takes a left-hand operand of type 'const std::basic_string<_Elem,_Traits,_Alloc>' (or there is no acceptable conversion) c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility 3171


一直找不到错误  很纠结。后来在http://stackoverflow.com/questions/8792110/stdsetstring-customcomparer-cannot-use-stdfind-with里看到类似的,可能是==没处理,而且是string的==

后来解决方式很雷人---加了#include<string.h>就可以了


国内的网上我没有搜到这个error,希望遇到这个问题的孩子们节省点时间 呵呵


数组和list都可以实现:

 数组 string Funlist[343]={。。。。。。}

     string *p = find(Funlist, Funlist + 342, strTmp);

if (p == Funlist + 342)
{
 strItemName =  prefix + Substr ;
   }
  else
  {  //not adfin
     strItemName = prefix1 + Substr;
  }

不过数组一定不要越界了

 使用容器

 list<string>::iterator it;

it = find(vecList.begin(),vecList.end(),strTmp);
/*cout << strTmp.c_str() << endl;
_getch();*/
//if (it == vecList.end())
//  {
//  strItemName =  prefix + Substr ;
// }
//  else
//  {  //not adfin
//     strItemName = prefix1 + Substr;
//  }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值