STL链接列表问题
STL linked list issues
我的代码中的逻辑似乎是错误的。该规则规定,对于一个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
类型的对象。
更重要的是,你似乎没有把逻辑想清楚。
要从列表中删除项目,它必须满足三个条件。
- 项目为
e
- 项目是单词的最后一个字母
- 单词的长度必须为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 == ' ');
}
相关文章:
- 在没有参数列表的情况下使用模板名称"Event"无效,模板问题
- C++关于ENUM的问题。我得到的响应比枚举列表大
- 替代在python中制作邻接列表与图形问题的字典?(如 C++ 中的 vector<vector<int&g
- 在C++中循环访问自定义结构列表的小问题
- 关于Stroustrup C++书的自动和{}列表问题
- 将值推送到 getter 函数调用的列表中时出现问题
- 使用初始化列表填充C++中的多维结构数组时出现问题
- 在 c++ 中访问列表向量时出现问题
- 有关具有编译错误的操纵列表的问题
- C++:继承和列表函数作为类成员的问题
- C++ 入门加列表:10.9 "顶部"指针问题
- 尝试使用初始值设定项列表构造"std::vector"的问题
- 自己的C++列表类实现(插入函数)出现问题
- 链接的列表字符串分类问题
- C 链接列表和总和问题
- C++有问题的函数,数组到列表
- 从一系列问题列表中生成输入(x)问题
- 析构函数问题 + 列表显示
- 显示条件有问题?(列表中的对象)
- 信号发出问题-列表视图没有显示完整的列表