如何根据对象成员值从列表中查找对象 在C++中,如果对象成员与特定值匹配,则希望在列表中找到对象。 class gate{ public: std::string type; } class netlist { std::list<gate *> gates_; void identify_out_gate(); }

如何根据对象成员值从列表中查找对象 在C++中,如果对象成员与特定值匹配,则希望在列表中找到对象。 class gate{ public: std::string type; } class netlist { std::list<gate *> gates_; void identify_out_gate(); },c++,c++11,C++,C++11,现在我想根据类型从列表中找到一个特定的门。我正在使用以下工具: netlist::identify_out_gate() { for (std::list<gate *>::const_iterator out_gate = gates_.begin(); out_gate != gates_.end(); ++out_gate) { if((*out_gate)->type == "output") {

现在我想根据类型从列表中找到一个特定的门。我正在使用以下工具:

netlist::identify_out_gate()
{
    for (std::list<gate *>::const_iterator out_gate = gates_.begin(); out_gate != gates_.end(); ++out_gate)
    {
        if((*out_gate)->type == "output")
        {
            //do something......
        }
    }
}
但我想知道我是否可以使用类似“查找”或“查找如果”和“如何查找”之类的功能?

当然可以。例如:

auto iter = std::find_if(gates_.begin(),
                         gates_.end  (),
                         [](gate const *g) { return g->type == "output"); });
iter的类型将是std::list::iterator


请注意,这只找到一个元素。要查找下一个,请使用find\u ifiter+1,gates\u.end。。。。此外,确保检查iter!=GATES.E.Enter,因为如果ITER=GATES.E.Enter,则没有发现任何东西。

查找和FundIf是C++标准的一部分。我建议您首先尝试自己解决问题,例如通过咨询。您好,Wintermute,谢谢您的回复,但当我实现此I gpt时,出现此错误:之前需要主表达式'['token..我遗漏了什么吗?你的编译器必须知道lambda闭包的C++11。如果你使用的是gcc或clang,如果你使用的是旧版本,请传递-std=C++11或-std=C++0x。如果你不想使用C++11,尽管你应该!你可以使用函数bool is_output\u gategate const*g{return g->type==output;}是输出门,而不是lambda。