自定义类上的C ++ std :: find_if

时间:2018-04-25 23:50:02

标签: c++ vector find

我有一个自定义类(Term)的std :: vector。我试图使用std :: find_if结构在该类中查找术语。我错过了一些东西:

Term.h

FOR %%f IN (D:\Folder1\*.jpeg) DO (
        move %%f B:\
        timeout /t 1 /nobreak
    )

然后,在代码的后面,我有另一个类(Document),我将在那里比较这些术语的向量: Document.cpp

 

    #ifndef _TERM_H_
    #define _TERM_H_

    #include <string>

    class Term {
        public:
                Term() {}
                Term(std::string, float);

                void setTerm(std::string myTerm) {this->term   = myTerm;}
                void setWeight(float myWeight) {this->weight = myWeight;}

                std::string getTerm() const {return this->term;}
                float getWeight() {return this->weight;}

                bool operator<(Term &t) {return t.getWeight() <  this->getWeight();}
                bool operator==(const Term *t) const {return this->getTerm().compare(t->getTerm()) == 0;}

        private:
                std::string term;
                float weight;
    }; // end class

    #endif

我很担心lambda表达式应该是什么样的,以找到相同的术语。我尝试了std :: find路由(注释掉了),但它从未找到两个术语匹配的时间。我相信find_if是正确的功能,但我现在正在与lambda挣扎。

有人能指出我正确的方向吗?谢谢!

1 个答案:

答案 0 :(得分:0)

我明白了。 std :: find_if行必须如下所示:

auto it = find_if(terms.begin(), terms.end(),
                              [&term](const Term *t1){return t1->getTerm() == term->getTerm();});