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

来源:互联网 发布:数据库概论第五版答案 编辑:程序博客网 时间:2024/06/06 03:57

先查了下资料 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 40The 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;
//  }