简体   繁体   English

在迭代器中从std :: list中删除条目

[英]Remove an entry out of a std::list while iterator

I am trying to remove elements in a list when a condition is met in a std::list . 我正在尝试在std::list满足条件时删除列表中的元素。 What I have read in the Reference about the return value of the erase function: 我在参考书中阅读了有关erase功能返回值的内容:

An iterator pointing to the element that followed the last element erased by the function call. 一个迭代器,指向由函数调用删除的最后一个元素之后的元素。 This is the container end if the operation erased the last element in the sequence. 如果操作删除了序列中的最后一个元素,则这是容器的结尾。

Member type iterator is a bidirectional iterator type that points to elements. 成员类型迭代器是指向元素的双向迭代器类型。

I have put this example together: 我把这个例子放在一起:

#include <string>
#include <list>
#include <iostream>

int main()
{
        typedef std::list<std::string> string_list_t;
        string_list_t list;
        list.push_back("test1");
        list.push_back("test2");
        list.push_back("test3");
        list.push_back("test4");
        list.push_back("test5");
        list.push_back("test6");
        list.push_back("test7");
        list.push_back("test8");

        for (string_list_t::iterator it = list.begin(); it != list.end(); ++it)
        {
                std::string &str = *it;
                std::cout << "Checking " << str << "..." << std::endl;
                if (str == "test4")
                {
                        std::cout << "Found test4!" << std::endl;
                }
                else
                {
                        it = list.erase(it);
                }
        }


    return 0;
}

It does not give me the expected output, instead it gives me: 它没有给我期望的输出,而是给了我:

Checking test1...
Checking test3...
Checking test5...
Checking test7...

Can someone help me figuring out what I understood wrong? 有人可以帮我弄清楚我理解错了吗? Somehow it skips every second element... 它以某种方式跳过了第二个元素...

You are skipping the element after the deleted one. 您正在删除已删除元素之后的元素。

You should use either it = list.erase(it); 应该使用it = list.erase(it); or ++it , but not both. ++it ,但不能同时使用。

When you erase an element from a std::list , do not increment the iterator returned by the std::list::erase method. std::list擦除元素时,不要增加std::list::erase方法返回的迭代器。 You will not just skip the next element, you may just end up incrementing an end() iterator. 您不仅可以跳过下一个元素,还可以最终增加一个end()迭代器。

Change your loop to: 将循环更改为:

for (string_list_t::iterator it = list.begin(); it != list.end(); )
                                                                //^^ Not incremented
        {
                std::string &str = *it;
                std::cout << "Checking " << str << "..." << std::endl;
                if (str == "test4")
                {
                        std::cout << "Found test4!" << std::endl;
                        ++it;           //increment
                }
                else
                {
                        it = list.erase(it);
                }
        }

See it Live Here 在这里看到它

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

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