std :: set <string,customcomparer =“”>不能使用std :: find(...)和</string,>

时间:2012-01-09 16:55:10

标签: c++ compiler-errors find set

我有以下无法编译的代码:

#include <iostream>
#include <set>
#include <functional>
#include <cstring>

using namespace std;

struct StringCompareNoRegister: public binary_function<string, string, bool> {
  bool operator()(string const& lhs, string const& rhs) const {
    return (_stricmp(lhs.c_str(), rhs.c_str()) < 0);
  }
};

int wmain() {
  set<string, StringCompareNoRegister> s;
  s.insert("hello");
  s.insert("STL");
  s.insert("Hello");
  wcout << s.find("Hello")->c_str() << endl;
  wcout << find(s.begin(), s.end(), "Hello")->c_str() << endl;

  return 0;
}

MVCPP v.11 CTP编译器在使用std::find的最后一行大喊:

  

错误1错误C2678:二进制'==':找不到哪个运算符需要a   'const的左手操作数   的std :: basic_string的&LT; _Elem,_Traits,_Alloc&GT;” (或者没有可接受的   转换)c:\ program files(x86)\ microsoft visual studio   11.0 \ vc \ include \ xutility 3171

为什么我无法编译此代码? 我做错了什么?

更新:完整编译器输出

  

1&gt; ------ Build build:Project:Test01,Configuration:Debug Win32   ------ 1&gt; main.cpp 1&gt; c:\ program files(x86)\ microsoft visual studio 11.0 \ vc \ include \ xutility(3171):错误C2678:二进制'==':找不到哪个运算符带有'的左手操作数'常量   的std :: basic_string的&LT; _Elem,_Traits,_Alloc&GT;” (或者没有可接受的   转换)1>用1> [1> _Elem =炭,   1 GT; _Traits = std :: char_traits,1&gt;
  _Alloc = std :: allocator 1&gt; ] 1&gt;可以是'内置C ++运算符==(const char [6],const char [6])'1&gt;
  c:\ program files(x86)\ microsoft visual studio   11.0 \ _vc \ include \ exception(488):或'bool std :: operator ==(const std :: _ Exception_ptr&amp;,const std :: _ Exception_ptr&amp;)'1&gt;

  c:\ program files(x86)\ microsoft visual studio   11.0 \ vc \ include \ exception(493):或'bool std :: operator ==(std :: _ Null_type,const std :: _ Exception_ptr&amp;)'1&gt; c:\ program files(x86)\ microsoft visual studio   11.0 \ vc \ include \ exception(499):或'bool std :: operator ==(const std :: _ Exception_ptr&amp;,std :: _ Null_type)'1&gt; c:\ program files   (x86)\ microsoft visual studio 11.0 \ vc \ include \ system_error(419):或
  'bool std :: operator ==(const std :: error_code&amp;,const   std :: error_condition&amp;)'1&gt; c:\ program files(x86)\ microsoft   visual studio 11.0 \ vc \ include \ system_error(427):或'bool   std :: operator ==(const std :: error_condition&amp;,const std :: error_code   &amp;)'1&gt; c:\ program files(x86)\ microsoft visual studio   11.0 \ vc \ include \ tuple(537):或'bool std :: operator ==(const std :: tuple&lt;&gt;&amp;,const std :: tuple&lt;&gt;&amp;)'1&gt;在尝试时   匹配参数列表'(const   std :: basic_string&lt; _Elem,_Traits,_Alloc&gt;,const char [6])'1&gt;

  用1> [1> _Elem = char,1&gt;
  _Traits = std :: char_traits,1&gt; _Alloc = std :: allocator 1&gt; ] 1&gt; c:\ program files(x86)\ microsoft visual studio 11.0 \ vc \ include \ xutility(3204):see   引用函数模板实例化'_InIt   的std :: _查找,常量   char [6]&gt;(_ InIt,_InIt,_Ty(&amp;))'被编译1&gt; 1 >>   [1>   _Init =标准:: _ Tree_unchecked_const_iterator,性病::分配器&GT;&GT;&GT;&gt;中   1 GT;
  _Mytree =标准:: _ Tree_val,性病::分配器&GT;&GT;&gt;中   1 GT; _Ty = const char [6] 1&gt; ] 1&gt;
  d:\ docs \ programming \ test01 \ test01 \ main.cpp(39):参见参考资料   函数模板实例化'_InIt   的std ::发现,常量   char [6]&gt;(_ InIt,_InIt,_Ty(&amp;))'被编译1&gt; 1 >>   [1>   _Init =标准:: _ Tree_const_iterator,性病::分配器&GT;&GT;&GT;&gt;中   1 GT;
  _Mytree =标准:: _ Tree_val,性病::分配器&GT;&GT;&gt;中   1 GT; _Ty = const char [6] 1&gt; ]   ==========构建:0成功,1个失败,0个最新,0个跳过==========

3 个答案:

答案 0 :(得分:3)

行动。 确切的答案是我忘了将<string>标题添加到包含。 字符串标头包含外部(非成员)std::string比较函数,如operator==operator<等。

非常感谢您的回答。

答案 1 :(得分:2)

std :: find不会以相同的方式使用自定义比较器。您需要重载==运算符。

预期行为如下所示。检查cplusplus.com以获取参考。

template<class InputIterator, class T>
  InputIterator find ( InputIterator first, InputIterator last, const T& value )
  {
    for ( ;first!=last; first++) if ( *first==value ) break;
    return first;
  }

如果您使用自定义类型,那么您会期望这样的事情。

struct Foo {
    Foo(const std::string &s_) : s(s_) {}
    std::string s;
    // used by std::set<Foo, Foo::Less>::find
    struct Less {
        bool operator()(const Foo &lhs, const Foo &rhs) const {
            return lhs.s.compare(rhs.s) < 0;
        }
    };
};

// used by std::find
bool operator==(const Foo &lhs, const Foo &rhs) {
    return lhs.s.compare(rhs.s) == 0;
}

int main(int argc, char ** argv) {
    std::set<Foo, Foo::Less> foos;
    foos.insert(Foo("hello"));
    foos.insert(Foo("STL"));
    foos.insert(Foo("Hello"));

    std::cout << foos.find(Foo("Hello"))->s.c_str() << std::endl;
    std::cout << find(foos.begin(), foos.end(), Foo("Hello"))->s << std::endl;
    return 0;
}   

Linux刚刚找到了std :: string的运算符,所以我无法测试你的具体行为。您可能需要添加一些包含。

#include <algorithm>
#include <string>

否则,定义自己的东西非常轻松。

bool operator==(const std::string &lhs, const std::string &rhs) {
    return lhs.compare(rhs) == 0;
}

答案 2 :(得分:0)

嗯,尝试将您的算子args更改为bool operator()(const string & lhs, const string & rhs)。看看是否有帮助。