STL链接列表问题

STL linked list issues

本文关键字:问题 列表 链接 STL      更新时间:2023-10-16

我的代码中的逻辑似乎是错误的。该规则规定,对于一个3个或更多字母单词末尾的每一个"e",都必须删除"e"。例如,"remove"将改为"remov",或者另一个例子是"ease"将更改为"eas"。我尝试使用myList.size-1,但是我遇到了一个编译错误。有人能帮忙吗?

为了方便起见,我会把错误的地方记下来。

#include <iostream>
#include <list>
#include <ctype.h>
#include <fstream>
using namespace std;
void printList(const list<char> &myList);
void fillList(list<char> &myList);
void change(list <char> &myList);
void printList(const list<char> &myList)
{
    list<char>::const_iterator itr;
    for (itr = myList.begin(); itr != myList.end(); itr++ )
    {
        cout <<*itr;
    }
    cout << 'n' << endl;
}
void fillList(list<char> &myList)
{
    ifstream file("test.txt");
    string print;
    while(file >> print)
    {
        for (int i = 0; i<=print.length(); i++)
        {
            myList.push_back(print[i]);
        }
        myList.push_back(' ');
    }
}
void change(list <char> &myList)
{
    list<char>::iterator itr;
    //rules are as follows
    //change w with v
    for (itr = myList.begin(); itr != myList.end(); itr++ )
    {
        if (*itr == 'w')
        {
            *itr = 'v';
        }
    }
    //remove e at the end of a 3+ letter word
    //PROBLEM HERE
    for (itr = myList.begin(); itr != myList.end(); itr++ )
    {
        std::list<char>::iterator itr2 = myList.size() - 1;
        if(myList.size() > 3 && itr2 == 'e')
        {
            myList.erase(itr2);
        }
    }
}
int main ()
{
    list<char> myList;
    ifstream file("test.txt");
    const string print;
    fillList(myList);
    printList(myList);
    change(myList);
    printList(myList);
    return 0;
}

您正在使用以下逻辑删除e s。

for (itr = myList.begin(); itr != myList.end(); itr++ )
{
    std::list<char>::iterator itr2 = myList.size() - 1;
    if(myList.size() > 3 && itr2 == 'e')
    {
        myList.erase(itr2);
    }
}

线路

    std::list<char>::iterator itr2 = myList.size() - 1;

导致编译器错误,因为赋值运算符的RHS类型为size_t。不能使用size_t初始化std::list<char>::iterator类型的对象。

更重要的是,你似乎没有把逻辑想清楚。

要从列表中删除项目,它必须满足三个条件。

  1. 项目为e
  2. 项目是单词的最后一个字母
  3. 单词的长度必须为3或更大

第一个检查很简单。

if ( *itr == 'e' )

第二次检查稍微复杂一些。

auto next = std::next(itr);
if ( *next == '' )

第三个检查比前两个都要复杂一点。由于您的列表在下一个单词开始之前嵌入了空字符和空格字符,因此您需要保留一个单词长度计数器,并在看到空格字符时重置它。

以下是实现上述逻辑的代码块:

int wordLength = 1;
// Don't use ++iter in the last clause of the for statement.
// iter is incremented only when the item is not removed.
// It is updated differently when the item is removed.
for (itr = myList.begin(); itr != myList.end(); )
{
   if ( needToRemove(itr, wordLength) )
   {
      // Remove the item.          
      itr = myList.erase(itr);
   }
   else
   {
      ++itr;
      ++wordLength;
   }
   if ( *itr == ' ' )
   {
      // Reset word length. The next character is
      // going to be the start of the next word.
      wordLength = 0;
   }
}

其中needToRemove定义为:

bool needToRemove(list<char>::iterator itr,
                  int wordLength) 
{
   if ( *itr != 'e' || wordLength < 3)
   {
      return false;
   }
   auto next = std::next(itr);
   return (*next == '');
}