std::set<字符串 ,customcomparer>不能将 std::find(...) 与

发布于 12-25 21:38 字数 3414 浏览 4 评论 0原文

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

#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<_Elem,_Traits,_Alloc>' (或者没有可接受的 转换)c:\program files (x86)\microsoft Visual Studio 11.0\vc\include\xutility 3171

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

更新:完整的编译器输出

1>------ 构建已开始:项目:Test01,配置:调试 Win32 ------ 1> main.cpp 1>c:\program files (x86)\microsoft Visual Studio 11.0\vc\include\xutility(3171): 错误 C2678: 二进制 '==' : 找不到采用类型 ' 的左操作数的运算符常量 std::basic_string<_Elem,_Traits,_Alloc>' (或者没有可接受的 换算)1>与 1> [1> _Elem=字符, 1> _Traits=std::char_traits, 1>
_Alloc=std::分配器1> ]1>可以是“内置 C++ 运算符 ==(const char [6], const char [6])”1>
c:\program files (x86)\microsoft Visual Studio 11.0\vc\include\exception(488): 或 'bool std::operator ==(const std::_Exception_ptr &,const std::_Exception_ptr &)' 1>
c:\program files (x86)\microsoft Visual Studio 11.0\vc\include\exception(493): 或 'bool std::operator ==(std::_Null_type,const std::_Exception_ptr &)' 1> c:\program files (x86)\microsoft Visual Studio 11.0\vc\include\exception(499): 或 'bool std::operator ==(const std::_Exception_ptr &,std::_Null_type)' 1> c:\程序文件 (x86)\microsoft Visual Studio 11.0\vc\include\system_error(419):或
'bool std::operator ==(const std::error_code &,const std::error_condition &)' 1> c:\program files (x86)\microsoft Visual Studio 11.0\vc\include\system_error(427): 或 'bool std::operator ==(const std::error_condition &,const std::error_code &)'1> c:\program files (x86)\microsoft Visual Studio 11.0\vc\include\tuple(537): 或 'bool std::operator ==(const std::tuple<> &,const std::tuple<> &)' 1>当试图 匹配参数列表 '(const std::basic_string<_Elem,_Traits,_Alloc>, const char [6])' 1>
与 1> [1> _Elem=char, 1>
_Traits=std::char_traits, 1> _Alloc=std::分配器1> ]1> c:\program files (x86)\microsoft Visual Studio 11.0\vc\include\xutility(3204) :参见 引用函数模板实例化'_InIt std::_Find,const char[6]>(_InIt,_InIt,_Ty (&))' 正在编译 1>与 1>
[ 1>
_InIt=std::_Tree_unchecked_const_iterator,std::分配器>>>, 1>
_Mytree=std::_Tree_val,std::分配器>>, 1> _Ty=const char[6]1> ] 1>
d:\docs\programming\test01\test01\main.cpp(39) :参见参考 函数模板实例化'_InIt std::查找、常量 char[6]>(_InIt,_InIt,_Ty (&))' 正在编译 1>与 1>
[ 1>
_InIt=std::_Tree_const_iterator,std::分配器>>>, 1>
_Mytree=std::_Tree_val,std::分配器>>, 1> _Ty=const char[6]1> ] ========== 构建:0 成功,1 失败,0 最新,0 跳过 ==========

I have the following code that cannot be compiled:

#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 compiler yelling on the last line where std::find is used:

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

Why I cannot compile this code?
What have I done wrong?

UPDATE: Full compiler output

1>------ Build started: Project: Test01, Configuration: Debug Win32
------ 1> main.cpp 1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(3171): 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) 1> with 1> [ 1> _Elem=char,
1> _Traits=std::char_traits, 1>
_Alloc=std::allocator 1> ] 1> could be 'built-in C++ operator==(const char [6], const char [6])' 1>
c:\program files (x86)\microsoft visual studio
11.0\vc\include\exception(488): or 'bool std::operator ==(const std::_Exception_ptr &,const std::_Exception_ptr &)' 1>
c:\program files (x86)\microsoft visual studio
11.0\vc\include\exception(493): or 'bool std::operator ==(std::_Null_type,const std::_Exception_ptr &)' 1> c:\program files (x86)\microsoft visual studio
11.0\vc\include\exception(499): or 'bool std::operator ==(const std::_Exception_ptr &,std::_Null_type)' 1> c:\program files
(x86)\microsoft visual studio 11.0\vc\include\system_error(419): or
'bool std::operator ==(const std::error_code &,const
std::error_condition &)' 1> c:\program files (x86)\microsoft
visual studio 11.0\vc\include\system_error(427): or 'bool
std::operator ==(const std::error_condition &,const std::error_code
&)' 1> c:\program files (x86)\microsoft visual studio
11.0\vc\include\tuple(537): or 'bool std::operator ==(const std::tuple<> &,const std::tuple<> &)' 1> while trying to
match the argument list '(const
std::basic_string<_Elem,_Traits,_Alloc>, const char [6])' 1>
with 1> [ 1> _Elem=char, 1>
_Traits=std::char_traits, 1> _Alloc=std::allocator 1> ] 1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(3204) : see
reference to function template instantiation '_InIt
std::_Find,const
char[6]>(_InIt,_InIt,_Ty (&))' being compiled 1> with 1>
[ 1>
_InIt=std::_Tree_unchecked_const_iterator,std::allocator>>>>,
1>
_Mytree=std::_Tree_val,std::allocator>>>,
1> _Ty=const char [6] 1> ] 1>
d:\docs\programming\test01\test01\main.cpp(39) : see reference to
function template instantiation '_InIt
std::find,const
char[6]>(_InIt,_InIt,_Ty (&))' being compiled 1> with 1>
[ 1>
_InIt=std::_Tree_const_iterator,std::allocator>>>>,
1>
_Mytree=std::_Tree_val,std::allocator>>>,
1> _Ty=const char [6] 1> ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寻梦旅人2025-01-01 21:38:16

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

非常感谢为您解答。

Ops.
The precise answer is that I've forgotten to add <string> header to includes.
String header contains external (non-member) std::string comparison functions like operator==, operator<, etc.

Thank you very much for your answers.

甜味超标?2025-01-01 21:38:16

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;
}

std::find does not use custom comparators in the same way. You need to overload the == operator.

The behavior is expected something like the below. Check cplusplus.com for the reference.

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

So you would expect something like this if you were using a custom type.

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 just found the operator for std::string, so I couldn't test your specific behavior. You may need to add some includes.

#include <algorithm>
#include <string>

Otherwise, defining your own is pretty painless.

bool operator==(const std::string &lhs, const std::string &rhs) {
    return lhs.compare(rhs) == 0;
}
无敌元气妹2025-01-01 21:38:16

嗯,尝试将函子参数更改为 bool operator()(const string & lhs, const string & rhs)。看看是否有帮助。

Hmm, try changing your functor args to bool operator()(const string & lhs, const string & rhs). See if that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文