std::set<字符串 ,customcomparer>不能将 std::find(...) 与
我有以下无法编译的代码:
#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 技术交流群。

发布评论
评论(3)
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::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 likeoperator==
,operator<
, etc.Thank you very much for your answers.