I am running into some unexpected string behavior when I append an Iterator to a string. Basically, I have a file that reads something along the lines of:
int x := 10;
print x;
I have a string already that contains the contents of this file, and I am iterating through it and simply removing whitespace right now.
void Lexer::parse()
{
Pointer = Filestring.begin(); // both private members
while(Pointer < Filestring.end())
{
if(is_whitespace(0)) // 0 indicates current Pointer position
{
advance(Pointer, 1);
continue;
}
CurToken.append(&*Pointer); // CurToken is private member string
advance(Pointer, 1);
}
cout << CurToken << endl;
}
By the end of the loop I would expect CurToken to be a string that contains nothing but the characters with all whitespace removed. Instead I get something like the following:
int x := 1 + 2;
print x;
nt x := 1 + 2;
print x;
t x := 1 + 2;
print x;
...
rint x;
int x;
nt x;
t x;
x;
;
I assume the issue is de-referencing the pointer, and if so, how can I append the current pointer position's character? If I do not de-refrence it, I end up with invalid conversion from ‘char’ to ‘const char*’
Note: is_whitespace() I have tested, and works as expected. So that can be ruled out as a possible problem.
itinstead ofPointerand do++itinstead ofadvance(Pointer,1)? Also, why notstd::isspace(*it)instead ofis_whitespace(0)? You're unnecessarily making it complicated.fstream f("file.txt"); string s; while(f >> s) cout << s << endl;