gpt4 book ai didi

c++ - std::find 跨一组 shared_ptr

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:24:12 25 4
gpt4 key购买 nike

我确定我在这里做了一些愚蠢的事情,但我看不到它。为什么不能编译以下内容?

#include <algorithm>
#include <memory>
#include <vector>
#include <string>

// A class to play with. Encapsulates a name.
class StringClass
{
public:
StringClass(std::string const & name) : MyName(name)
{
}

std::string const & Name() const
{
return MyName;
}

private:
std::string MyName;
};

// The set of instances of "StringClass".

std::vector<std::shared_ptr<StringClass>> MyInstances;

// Function returns "true" if a class with the given name exists in the collection.
bool Exists(std::string const & name)
{
auto i = std::find(MyInstances.begin(), MyInstances.end(), [&](std::shared_ptr<StringClass> const & instance) {
return instance->Name() == name;
});

return i != MyInstances.end();
}

我已经将 shared_ptr 的 vector 创建到一个类中。该类有一个 Name() 属性。我想要做的就是迭代 vector 以查找具有给定名称的类的实例。但是,它无法编译 :(。

错误是:

1>ClCompile: 1> test.cpp 1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm(41): error C2679: binary '==' : no operator found which takes a right-hand operand of type 'const anonymous-namespace'::<lambda0>' (or there is no acceptable
conversion) 1> could be 'built-in C++
operator==(std::_Bool_type, std::_Bool_type)' 1> c:\program
files (x86)\microsoft visual studio 10.0\vc\include\exception(470): or
'bool std::operator ==(const std::_Exception_ptr &,const
std::_Exception_ptr &)' 1> c:\program files (x86)\microsoft
visual studio 10.0\vc\include\exception(475): or 'bool
std::operator ==(std::_Null_type,const std::_Exception_ptr &)' 1><br/>
c:\program files (x86)\microsoft visual studio
10.0\vc\include\exception(481): or 'bool std::operator ==(const std::_Exception_ptr &,std::_Null_type)' 1> c:\program files
(x86)\microsoft visual studio 10.0\vc\include\system_error(408): or<br/>
'bool std::operator ==(const std::error_code &,const
std::error_condition &)' 1> c:\program files (x86)\microsoft
visual studio 10.0\vc\include\system_error(416): or 'bool
std::operator ==(const std::error_condition &,const std::error_code
&)' 1> while trying to match the argument list
'(std::tr1::shared_ptr<_Ty>, const
anonymous-namespace'::)' 1> with 1> [ 1> _Ty=StringClass 1>
] 1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm(74) : see reference to function template instantiation '_InIt std::_Find*,anonymous-namespace'::<lambda0>>(_InIt,_InIt,const
anonymous-namespace':: &)' being compiled 1> with 1> [ 1> _InIt=std::tr1::shared_ptr *, 1> _Ty=StringClass 1> ] 1> c:\svn\trunk\test\test\test.cpp(55) : see reference to function template instantiation '_InIt std::find,anonymous-namespace'::<lambda0>>(_InIt,_InIt,const
_Ty &)' being compiled 1> with 1> [ 1> _InIt=std::_Vector_iterator<std::_Vector_val<std::tr1::shared_ptr<StringClass>,std::allocator<std::tr1::shared_ptr<StringClass>>>>, 1><br/>
_Myvec=std::_Vector_val<std::tr1::shared_ptr<StringClass>,std::allocator<std::tr1::shared_ptr<StringClass>>>,
1> _Ty=
anonymous-namespace':: 1> ] 1> stdafx.cpp 1> Generating Code... 1> 1>Build FAILED. 1> 1>Time Elapsed 00:00:00.87 ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

最佳答案

您想使用find_if,而不是findFind 搜索一个值,find_if 接受一个谓词。

关于c++ - std::find 跨一组 shared_ptr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14584917/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com