c++ - find_if 的 Lambda 表达式

标签 c++

我目前正试图在 vector V 中找到一个元素。 但我得到了很多错误。

bool VNS::remove(const HostName& name){ ^ 在/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;
};

接口(interface):

/*
 * 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

我的 lambda exp 有两个参数。编译器如何知道应该如何用正确的值替换它们。

最佳答案

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

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

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

reference page很好地解释了 lambda。

关于c++ - find_if 的 Lambda 表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21972179/

相关文章:

python - 在 DLL 中嵌入 Python : Access violation reading location when Py_DECREF list object

C++访问结构变量导致段错误

c++ - 从 BST 中删除唯一的节点

c++ - 将静态 C 库与 C++ 代码链接时出现 "undefined reference to"错误

c++ - 包括来自不同目录的头文件的问题[不是路径问题]

c++ - 外部 "C"DLL : Debug is OK, 发布抛出错误 C2059

c++ - 无法删除类的析构函数中指向数组的成员指针

c++ - 使用 Cmake 的 Blink 浏览器引擎示例?

c++ - 期望 const std::string & 的函数的链接器错误

c++ - 对于相对较新的计算机,有什么替代OL'C++ Beep()的好方法?