std ::查找一组shared_ptr

时间:2013-01-29 14:12:52

标签: c++ find containers

我确定我在这里做了些蠢事,但我看不到它。为什么以下没有编译?

#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的向量。该类具有Name()属性。我想要做的就是迭代向量,寻找具有给定名称的类的实例。但是,它没有编译:(。

错误是:

  

1&gt; ClCompile:1&gt; test.cpp 1&gt; c:\ program files(x86)\ microsoft visual   studio 10.0 \ vc \ include \ algorithm(41):错误C2679:二进制'==':否   找到的运算符采用'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>
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
'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
匿名命名空间 '::)'   1 GT;用1> [1> _Ty = StringClass 1&gt;
  ] 1&gt; c:\ program files(x86)\ microsoft visual studio   10.0 \ vc \ include \ algorithm(74):参见函数模板实例化'_InIt   std :: _ Find *,anonymous-namespace'::<lambda0>>(_InIt,_InIt,const anonymous-namespace'::&amp;)'正在编译1&gt;同   1 GT; [1> _Init =标准:: TR1 :: shared_ptr的   *,1&gt; _Ty = StringClass 1&gt; ] 1&gt; c:\ svn \ trunk \ test \ test \ test.cpp(55):参见函数参考   模板实例化'_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>
_Myvec=std::_Vector_val<std::tr1::shared_ptr<StringClass>,std::allocator<std::tr1::shared_ptr<StringClass>>>, 1> _Ty=
anonymous-namespace':: 1&gt; ] 1&gt;   stdafx.cpp 1&gt;生成代码... 1&gt; 1&gt; Build FAILED。 1 GT; 1&gt;经过的时间   00:00:00.87   ==========构建:0成功,1个失败,0个最新,0个跳过==========

1 个答案:

答案 0 :(得分:8)

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