C++ find_if的Lambda表达式

C++ find_if的Lambda表达式,c++,C++,我目前正试图在向量V中找到一个元素。 但是我犯了很多错误 bool VNS::remove(常量主机名和名称){ ^ 在/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/algorithm:62:0中包含的文件中, 从vns.cc:2: bool VNS::remove(const HostName& name){ auto it=find_if(v.begin(),v.end(),[](const HostName&

我目前正试图在向量V中找到一个元素。 但是我犯了很多错误

bool VNS::remove(常量主机名和名称){ ^ 在/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/algorithm:62:0中包含的文件中, 从vns.cc:2:

 bool VNS::remove(const HostName& name){
        auto it=find_if(v.begin(),v.end(),[](const HostName& a, const HostName& b){return a==b;});
        //code that will remove the elem.
        if(it!=v.end()){
            return true;
        }else{
            return false;
        }
    }
HeaderFile:
class VNS:public NameServerInterface{
    public:
        /*
         * Insert a name/address pair. Does not check if the name
         * or address already exists.
         */
        virtual void insert(const HostName&, const IPAddress&);

        /*
         * Remove the pair with the specified host name. Returns true
         * if the host name existed and the pair was removed, false
         * otherwise.
         */
        virtual bool remove(const HostName&);

        /*
         * Find the IP address for the specified host name. Returns
         * NON_EXISTING_ADDRESS if the host name wasn't in the name
         * server.
         */
        virtual IPAddress lookup(const HostName&) const;

    private:
        std::vector<std::pair<HostName,IPAddress> > v;
};
bool VNS::remove(常量主机名和名称){
autoit=find_if(v.begin(),v.end(),[](常量主机名&a,常量主机名&b){返回a==b;});
//删除元素的代码。
如果(it!=v.end()){
返回true;
}否则{
返回false;
}
}
头文件:
类VNS:公共名称服务器接口{
公众:
/*
*插入名称/地址对。不检查名称
*或地址已存在。
*/
虚拟空插入(常量主机名和常量IP地址和);
/*
*删除具有指定主机名的对。返回true
*如果主机名存在且该对已删除,则为false
*否则。
*/
虚拟bool删除(const主机名&);
/*
*查找指定主机名的IP地址。返回
*如果主机名不在名称中,则不存在\u地址
*服务器。
*/
虚拟IP地址查找(常量主机名和)常量;
私人:
std::向量v;
};
接口:

/*
 * Interface NameServerInterface -- all name server implementations must
 * implement this interface.
 */
#ifndef NAME_SERVER_INTERFACE_H
#define NAME_SERVER_INTERFACE_H

#include <string>

using HostName = std::string;
using IPAddress = unsigned int;
const IPAddress NON_EXISTING_ADDRESS = 0;

class NameServerInterface {
public:
    virtual ~NameServerInterface() = default;

    /*
     * Insert a name/address pair. Does not check if the name
     * or address already exists.
     */
    virtual void insert(const HostName&, const IPAddress&) = 0;

    /*
     * Remove the pair with the specified host name. Returns true
     * if the host name existed and the pair was removed, false
     * otherwise.
     */
    virtual bool remove(const HostName&) = 0;

    /*
     * Find the IP address for the specified host name. Returns
     * NON_EXISTING_ADDRESS if the host name wasn't in the name
     * server.
     */
    virtual IPAddress lookup(const HostName&) const = 0;
};

#endif
/*
*接口名称服务器接口——所有名称服务器实现都必须
*实现这个接口。
*/
#ifndef名称\u服务器\u接口\u H
#定义名称\u服务器\u接口\u H
#包括
使用HostName=std::string;
使用IPAddress=unsigned int;
常量IPAddress非现有地址=0;
类名称服务器接口{
公众:
virtual~NameServerInterface()=默认值;
/*
*插入名称/地址对。不检查名称
*或地址已存在。
*/
虚拟空插入(常量主机名和常量IP地址-)=0;
/*
*删除具有指定主机名的对。返回true
*如果主机名存在且该对已删除,则为false
*否则。
*/
虚拟bool-remove(const-HostName&)=0;
/*
*查找指定主机名的IP地址。返回
*如果主机名不在名称中,则不存在\u地址
*服务器。
*/
虚拟IP地址查找(const主机名和)const=0;
};
#恩迪夫
我的lambda exp有两个参数。编译器如何知道应该如何用正确的值替换它们。

需要一元谓词。您正在向它传递一个二进制谓词:

这无法工作。看起来这是您真正想要的:

auto it = std::find(v.begin(),v.end(), name);
需要一元谓词。您正在传递一个二元谓词:

这无法工作。看起来这是您真正想要的:

auto it = std::find(v.begin(),v.end(), name);

若要
查找_if
,应传递一个“谓词”,即一个函数接受一个参数并返回
true
/
false

将lambda替换为

[&](const HostName& a){return a==name;}
应该像你期望的那样工作


正如其他注释所述,如果
find
,您可以将字符串传递给
find
,而不是将谓词传递给
find\u,因为
find
无论如何都使用
操作符==

如果
您应该传递一个“谓词”,即采用一个参数并返回
true
/
false
的函数

将lambda替换为

[&](const HostName& a){return a==name;}
应该像你期望的那样工作


正如其他注释所述,您可以将字符串传递给
find
,而不是
find\u if
的谓词,因为
find
无论如何都使用
运算符==

您不需要使用
std::find\u if
。使用
std::find

更改thsi语句

 auto it=find_if(v.begin(),v.end(),[](const HostName& a, const HostName& b){return a==b;});

编辑:很抱歉,我没有看到您将向量定义为

std::vector<std::pair<HostName,IPAddress> > v;
另外,由于成员函数
remove
具有返回类型
bool
,我建议使用算法
std::any\u
而不是
std::find_if

比如说

bool VNS::remove( const HostName &name )
{
   return any_of( v.begin(), v.end(), 
                  [&]( const std::pair<HostName,IPAddress>& a) { return a.first == name; } );    
}
有效代码将为

bool found = it != v.end();
if ( found )
{
   // removing the element
}

return found;

问题是您提供了一个令人困惑的代码示例。

如果
,则不需要使用
std::find\u。使用
std::find

更改thsi语句

 auto it=find_if(v.begin(),v.end(),[](const HostName& a, const HostName& b){return a==b;});

编辑:很抱歉,我没有看到您将向量定义为

std::vector<std::pair<HostName,IPAddress> > v;
另外,由于成员函数
remove
具有返回类型
bool
,我建议使用算法
std::any\u
而不是
std::find_if

比如说

bool VNS::remove( const HostName &name )
{
   return any_of( v.begin(), v.end(), 
                  [&]( const std::pair<HostName,IPAddress>& a) { return a.first == name; } );    
}
有效代码将为

bool found = it != v.end();
if ( found )
{
   // removing the element
}

return found;

问题是您提供了一个令人困惑的代码示例。:

您的错误消息转录遗漏了最有趣的部分:错误消息

然而,问题似乎很明显:
find_if
函数只需要一个参数,因此您需要捕获
名称,这就是
[]
的用途:

auto it=find_if(v.begin(),v.end(),
    [&name](const HostName& a){return a==name;});

这很好地解释了lambda。

您的错误消息转录遗漏了最有趣的部分:错误消息

然而,问题似乎很明显:
find_if
函数只需要一个参数,因此您需要捕获
名称,这就是
[]
的用途:

auto it=find_if(v.begin(),v.end(),
    [&name](const HostName& a){return a==name;});

这很好地解释了lambda。

你期望
std::find_if
作为值传入什么?你期望
std::find_if
作为值传入什么?但是如果我有一元谓词,那么我如何比较两件事呢?@user2975699,你基本上在做
std::find
循环中已经做的事情t使用
==
将它所在的对象与它正在寻找的对象进行比较。如果有帮助,您可以查看
std::find
的示例实现,并查看它为您自己做了什么。但是如果我有一元谓词,那么如何比较两件事呢?@user2975699,您基本上在做
std::find循环中已经做的事情ode>。它使用
==