C++ 如何使用字符串来查找对象,为指向对象的唯一指针向量返回迭代器?

C++ 如何使用字符串来查找对象,为指向对象的唯一指针向量返回迭代器?,c++,class,vector,iterator,unique-ptr,C++,Class,Vector,Iterator,Unique Ptr,我有一个List类,它包含一个向量,该向量具有指向ListItem对象的唯一指针。我想创建一个函数,将迭代器返回到特定的ListItem。将使用字符串参数与ListItem字符串名称变量进行比较 我尝试过使用std::find、find_if等,但没有成功。当我在向量上迭代时,我不知道如何访问ListItem对象中的name变量来进行比较 #include <iostream> #include <vector> #include <memory> #incl

我有一个List类,它包含一个向量,该向量具有指向ListItem对象的唯一指针。我想创建一个函数,将迭代器返回到特定的ListItem。将使用字符串参数与ListItem字符串名称变量进行比较

我尝试过使用std::find、find_if等,但没有成功。当我在向量上迭代时,我不知道如何访问ListItem对象中的name变量来进行比较

#include <iostream>
#include <vector>
#include <memory>
#include <string>
#include <iterator>
#include <algorithm>

class ListItem {
private:
    double quantity{ 0 };
    std::string weightUnit;
    std::string name;

public:
    ListItem(double quantity, std::string weightUnit, std::string name) : quantity{ quantity }, weightUnit{ weightUnit }, name{ name } {}
    ~ListItem() {}

    double getQuantity() { return quantity; }
    std::string getWeightUnit() { return weightUnit; }
    std::string getName() { return name; }

};

class List {
private:
    std::vector<std::unique_ptr<ListItem>>mylist;

public:
    std::vector<std::unique_ptr<ListItem>>::iterator search(std::string str) {
    /* This is where I'm stuck */

    }

    void removeListItem(std::string &name) {
        auto it = search(name);
        if (it != mylist.end()) {
            mylist.erase(it);
        }
    }

    void addListItem(double quantity, std::string weightUnit, std::string name) {

        mylist.push_back(std::make_unique<ListItem>(quantity, weightUnit, name));
    }

};



int main() {
    auto list = std::make_unique<List>();
    list->addListItem(2, "kg", "beef");
    list->addListItem(4, "lbs", "eggs");
    list->search("test");

}
#包括
#包括
#包括
#包括
#包括
#包括
类列表项{
私人:
双倍量{0};
std::字符串权重单位;
std::字符串名;
公众:
ListItem(双重数量,std::string weightUnit,std::string name):数量{quantity},重量单位{weightUnit},名称{name}{
~ListItem(){}
double getQuantity(){返回数量;}
std::string getWeightUnit(){return weightUnit;}
std::string getName(){return name;}
};
班级名单{
私人:
std::向量列表;
公众:
std::vector::迭代器搜索(std::string str){
/*这就是我被困的地方*/
}
void removeListItem(标准::字符串和名称){
自动it=搜索(名称);
if(it!=mylist.end()){
删除(它);
}
}
void addListItem(双倍数量,标准::字符串权重单位,标准::字符串名称){
mylist.push_back(标准::使_唯一(数量、重量单位、名称));
}
};
int main(){
自动列表=标准::使_唯一();
列表->添加列表项(2,“千克”,“牛肉”);
列表->添加列表项(4,“磅”,“蛋”);
列表->搜索(“测试”);
}

您需要捕获lambda中用作谓词的
str
参数

   std::vector<std::unique_ptr<ListItem>>::iterator search(std::string str) {
        auto tester = [str](std::unique_ptr<ListItem>& li) { return li->getName() == str; };
        return std::find_if(mylist.begin(), mylist.end(), tester);
    }
std::vector::迭代器搜索(std::string str){
自动测试仪=[str](std::unique_ptr&li){return li->getName()==str;};
返回std::find_if(mylist.begin()、mylist.end()、tester);
}

请注意,lambda中的参数类型将是
唯一的\u ptr
。(在c++14中,您可以只使用
auto

您可以演示如何使用
find\u if
?这是作业的“标准”工具,其中str是我传递给函数的字符串:return std::find_if(mylist.begin(),mylist.end(),[](ListItem a){return a.getName()==str;});您的向量包含
唯一的\u ptr
。这就是您的lambda需要(通过引用)的内容。我对此进行了更改,但仍然会出现错误,如果我无法找出如何将其与str参数进行比较:“return std::find_if(mylist.begin(),mylist.end(),[](const std::unique_ptr&a){return a->getName()=“beef”}”