[英]c++ STL linked list
Hey guys there seems to be an error with my code. 大家好,我的代码似乎有错误。
My main problem is with the section where I want to change any occurrence of 'p' followed by a 'h'. 我的主要问题是要更改任何出现的“ p”后跟“ h”的部分。 In my testing it changes it to a 'q' for some reason, when I want it to change to 'f'.
在我的测试中,出于某种原因,当我希望将其更改为“ f”时,它会将其更改为“ q”。
For convenience the method with the problem is the void change(), in the second for loop. 为方便起见,有问题的方法是第二个for循环中的void change()。
Can someone please help? 有人可以帮忙吗?
#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
for (itr = myList.begin(); itr != myList.end(); itr++ ) {
if (*itr == 'w' ){
*itr = 'v';
}
}
for (itr = myList.begin(); itr != myList.end(); itr++ ) {
if((*itr == 'p' && ++*itr == 'h')){// rule incomplete ask!
*itr = 'f';
}
}
}
int main ()
{
list<char> myList;
ifstream file("test.txt");
const string print;
fillList(myList);
printList(myList);
change(myList);
printList(myList);
return 0;
}
The code if((*itr == 'p' && ++*itr == 'h'))
does the following: 代码
if((*itr == 'p' && ++*itr == 'h'))
执行以下操作:
itr
is p
itr
值是p
itr
and increment it itr
的值并递增
p + 1 = q
what you need to do is increase the iterator, not the value 您需要做的是增加迭代器,而不是值
if(*itr == 'p') {
if(itr == myList.end()) break; // return or do something else
std::list<char>::iterator itr2 = itr;
if(*(++itr2) == 'h') {
// do what you need to
}
}
Edit: Fixed issue with random iterator and check if p
is last char as pointed out in the comments. 编辑:修复了随机迭代器的问题,并检查
p
是否为注释中指出的最后一个字符。
It's because you were incrementing the pointers character and not the iterator. 这是因为您要增加指针字符而不是迭代器。 You could fix this by doing
*++itr
instead of ++*itr
您可以通过
*++itr
代替++*itr
来解决此++*itr
I've changed your code up, you can easily make new rules. 我已经更改了代码,可以轻松制定新规则。
//we copy the string because we want to modify it without modifying the original
// http://stackoverflow.com/a/14679003/4376737
std::string find_and_replace(string str, const string& find, const string& replace)
{
size_t pos = 0;
while ((pos = str.find(find, pos)) != string::npos) {
str.replace(pos, find.length(), replace);
pos += replace.length();
}
return std::move(str);
}
void change(list <char> &myList)
{
list<char> newlist;
std::stringstream ss;
for (auto&& it = myList.begin(); it != myList.end(); ++it) {
if (*it != ' ') {
ss << *it;
} else {
auto&& newstr = find_and_replace(ss.str(), "ph", "f");
newstr = find_and_replace(newstr, "w", "v");
for (auto&& ch : newstr) {
newlist.push_back(ch);
}
newlist.push_back(' ');
std::stringstream().swap(ss); //this clears the stringstream
}
}
myList = newlist;
}
Output: 输出:
hamper moshpit phile wwwphwwwf
hamper moshpit file vvvfvvvf
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.