本文介绍了如果元素 id 与搜索参数匹配,如何从 std::vector 中删除元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个算法,如果项目 ID 与参数匹配,它将搜索并从项目向量中删除项目.请参阅下面的示例代码:

I'm trying to write an algorithm which will search for, and remove an item from a vector of items, if the item ID matches an argument. See example code below:

struct item{
    item(int newID){id = newID;}
    bool operator==(const item& other){return id = other.id;}
    int id
};

std::vector<std::unique_ptr<item>> vec;

vec.push_back(std::unique_ptr<item>(new item(10));
vec.push_back(std::unique_ptr<item>(new item(15));
vec.push_back(std::unique_ptr<item>(new item(20));

所以,使用上面的代码,我希望能够搜索存储值 15 的项目,并将其从向量中删除,删除它.

so, using the above code, I want to be able to search for the item which stores the value 15, and remove it from the vector, deleting it.

我该怎么做?

诚然,我可能也需要复习独特指针的使用,所以如果我的语法有误,请随时纠正我.

Admittedly, I probably need to brush up on the use of unique pointers too, so please feel free to correct me if my syntax is incorrect.

我尝试过的一些解决方案如下:

Some of the solutions I have attempted are as follows:

void remove_item(int id){
    vec.erase(
               std::remove_if(
                               vec.begin(),
                               vec.end(),
                               [](const item& e){
                                     return id==e.id;
                               }),
               vec.end()
              );

上面的代码产生一个错误,指出变量 id 不是 lambda 表达式的捕获列表的一部分.

The above code produces an error stating that the variable id is not part of the capture list for lambda expression.

其次,我尝试过:

void remove_item(item e){
    auto iter = std::find(vec.begin(), vec.end(), e);
    vec.erase(iter);
}

以上代码在这种情况下会在 == 运算符成员函数中产生类型不匹配错误.

The above code in this case produces type mismatch errors in the == operator member function.

推荐答案

您需要将 id 添加到 lambda 的捕获列表中,以便它可以访问它.然后,您需要使传递给 lambda 的类型成为 *vec.begin() 的类型,它是 std::unique_ptr 而不是 项目

You need to add id to the capture list of the lambda so it has access to it. Then you need to make the type passed to the lambda the type of *vec.begin() which is a std::unique_ptr<item> and not an item

void remove_item(int id){
    vec.erase(
               std::remove_if(
                               vec.begin(),
                               vec.end(),
                               [id](const std::unique_ptr<item>& e){
                                     return id==e->id;
                               }),
               vec.end()
              );
}

从您的代码中删除所有其他无关紧要的错误,您会得到类似的结果:

Removing all of the other extraneous error from you code you would have something like:

struct item {
    item(int newID) { id = newID; }
    bool operator==(const item& other) { return id == other.id; } // == here not =
    int id;
};

std::vector<std::unique_ptr<item>> vec;

void remove_item(int id) {
    vec.erase(std::remove_if(
        vec.begin(), vec.end(), [id](const std::unique_ptr<item>& e)
                                    {   return id == e->id; })
        ,vec.end());
}

int main()
{
    vec.push_back(std::unique_ptr<item>(new item(10))); // was missing a close paren
    vec.push_back(std::unique_ptr<item>(new item(15))); // was missing a close paren
    vec.push_back(std::unique_ptr<item>(new item(20))); // was missing a close paren
    remove_item(15);
    for (const auto & e : vec)
        std::cout << e->id << " ";
}

这篇关于如果元素 id 与搜索参数匹配,如何从 std::vector 中删除元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 03:36