如何删除字符串列表中的字符串

我试图按照列表上的示例迭代器的擦除示例进行操作,但我无法使其工作。

这是到目前为止的代码:

for (list<list<string>>::iterator itr = listOfList.begin(), ; itr != listeOfListe.end(); itr++){
  if (condition) {
    for (list<string>::iterator it6 = itr->begin(); it6 != itr->end(); it6++)
    {
      itr.erase(*it6);
    }
  }
}

我收到以下错误:

class "std::_List_iterator<std::__cxx11::list<std::string, std::allocator<std::string>>>" has no member "erase"

这表明它认为这itr是一个字符串列表,为什么不是itr简单的字符串列表,因为它迭代了listOfList

回答

您正在尝试调用erase()迭代器本身,而不是list迭代器所指的对象。这就是您收到编译器错误的原因。您需要通过operator*operator->访问list要调用的迭代器来取消引用erase()

另外,你的内循环不占的事实,list::erase() 使指定迭代器,让你的使用it6++未定义行为调用后erase(it6)。您需要使用erase()返回的迭代器才能正确地继续循环,例如:it6 = erase(it6)

尝试更像这样的事情:

// pre C++11
for (list<list<string>>::iterator itr = listOfList.begin(); itr != listOfList.end(); ++itr){
    if (condition) {
        list<string>::iterator it6 = itr->begin();
        while (it6 != itr->end()) {
            it6 = itr->erase(it6);
        }
    }
}

// C++11 and later
for (auto &listOfStrings : listOfList){
    if (condition) {
        auto it6 = listOfStrings.begin();
        while (it6 != listOfStrings.end()) {
            it6 = listOfStrings.erase(it6);
        }
    }
}

这简化为:

// pre C++11
for (list<list<string>>::iterator itr = listOfList.begin(); itr != listOfList.end(); ++itr){
    if (condition) {
        itr->clear();
    }
}

// C++11 and later
for (auto &listOfStrings : listOfList){
    if (condition) {
        listOfStrings.clear();
    }
}

或者:

// pre C++11
if (condition) {
    for (list<list<string>>::iterator itr = listOfList.begin(); itr != listOfList.end(); ++itr){
        itr->clear();
    }
}

// C++11 and later
if (condition) {
    for (auto &listOfStrings : listOfList){
        listOfStrings.clear();
    }
}

不过,我认为这不是你要找的。你的标题说你想“从列表中删除一个字符串”,所以你可能正在寻找更像这样的东西:

// pre C++11
for (list<list<string>>::iterator itr = listOfList.begin(); itr != listOfList.end(); ++itr){
    if (condition) {
        itr->remove(string); // removes all matching strings
    }
}

// C++11 and later
for (auto &listOfStrings : listOfList){
    if (condition) {
        listOfStrings.remove(string); // removes all matching strings
    }
}

或者:

// pre C++11
if (condition) {
    for (list<list<string>>::iterator itr = listOfList.begin(); itr != listOfList.end(); ++itr){
        itr->remove(string); // removes all matching strings
    }
}

// C++11 or later
if (condition) {
    for (auto &listOfStrings : listOfList){
        listOfStrings.remove(string); // removes all matching strings
    }
}

或者,也许您正在寻找更像这样的东西?

// pre C++11
for (list<list<string>>::iterator itr = listOfList.begin(); itr != listOfList.end(); ++itr){
    list<string>::iterator it6 = itr->begin();
    while (it6 != itr->end()) {
        if (condition(*it6)) {
            it6 = itr->erase(it6);
        } else {
            ++it6;
        }
    }
}

// C++11 and later
for (auto &listOfStrings : listOfList){
    auto it6 = listOfStrings.begin();
    while (it6 != itr->end()) {
        if (condition(*it6)) {
            it6 = listOfStrings.erase(it6);
        } else {
            ++it6;
        }
    }
}

在后一种情况下,在 C++20 中简化为:

for (auto &listOfStrings : listOfList){
    std::erase_if(listOfStrings, [](string &s){ return condition(s); });
}


以上是如何删除字符串列表中的字符串的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>