简体   繁体   English

从std :: list中删除std :: tuple

[英]Removing std::tuple from std::list

I have a list of tuples and need to remove elements from the list, something like this: 我有一个元组列表,需要从列表中删除元素,如下所示:

enum class test
{
    mem1,
    mem2,
    mem3,
    mem4
};

struct A
{

};

int main()
{
    std::list<std::tuple<int, test, A>> tuple_list;

    // fill list with random objects
    for (int i = 0; i < 4; i++)
    {
        tuple_list.push_back(
               std::forward_as_tuple(i, static_cast<test>(i), A()));
    }

    // now remove it
    for (auto& ref : tuple_list)
    {
        tuple_list.remove(ref); // error C2678
    }
    return 0;
}

error C2678: binary '==': no operator found which takes a left-hand operand of type 'const _Ty' (or there is no acceptable conversion) 错误C2678:二进制'==':找不到哪个运算符带有'const _Ty'类型的左操作数(或者没有可接受的转换)

How do I remove tuple elements from the list in above example? 如何从上面的示例中的列表中删除元组元素?

EDIT: 编辑:

I tried following method, it compiles just fine unlike previous example but there is runtime assertion: 我尝试了以下方法,它编译得很好,不像以前的例子,但有运行时断言:

int main()
{
    list<tuple<int, test, A>> tuple_list;

    for (int i = 0; i < 4; i++)
    {
        tuple_list.push_back(
                std::forward_as_tuple(i, static_cast<test>(i), A()));
    }

    for (auto iter = tuple_list.begin(); iter != tuple_list.end(); iter++)
    {
        tuple_list.erase(iter);
    }
}

Expression: can not increment value initialized list iterator 表达式:无法递增值初始化列表迭代器

First off, you don't want to do this . 首先, 你不想这样做 Removing items from a list (or any container) in the middle of a range-based for is a recipe for disaster as hidden behind the for loop are iterators that will be invalidated as soon as the item is removed. 从删除的项目list中的中间(或任何容器)范围为基础for是一个灾难的背后隐藏着for循环是迭代器 ,将尽快项被删除无效。

This is the same problem as the second experiment with 这与第二次实验的问题相同

for (auto iter = tuple_list.begin(); iter != tuple_list.end(); iter++)
{
    tuple_list.erase(iter); // iter rendered invalid. 
                            // That makes iter++ and iter != tuple_list.end()
                            // totally bogus.
}

This version can be fixed with 这个版本可以修复

for (auto iter = tuple_list.begin(); iter != tuple_list.end(); /* nothing here */)
{
    iter = tuple_list.erase(iter); // iter updated here
}

or a 或者a

while (! tuple_list.empty()) 
{
     tuple_list.pop_front();
}

Or 要么

tuple_list.clear();

OK. 好。 On to what went wrong: 到底出了什么问题:

error C2678: binary '==': no operator found which takes a left-hand operand of type 'const _Ty' (or there is no acceptable conversion) 错误C2678:二进制'==':找不到哪个运算符带有'const _Ty'类型的左操作数(或者没有可接受的转换)

means that one of the parts of the tuple cannot be compared for equality. 表示无法比较元组的其中一个部分是否相等。

struct A
{

};

has no equality operator. 没有相等的运算符。 Solution is to add one. 解决方案是添加一个。

struct A
{
}; 

bool operator==(const A& lhs, const A& rhs)
{ 
    Comparison logic goes here 
}    

Useful additional reading: 有用的额外阅读:

The Erase-Remove idiom can be used to solve similar problems. 擦除删除成语可用于解决类似问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM