I have a pretty simple one for you today, but it certainly has had me stumped for hours. I imagine there is something wrong with the subtitles of my string iterator. I've looked online and even passed the code to my CSE professor - but what with it being thanksgiving break, he only had his phone and wasn't able to be of much help.
Hopefully someone can look at this and immediately spot the problem. As a note, my goal is simply to remove special characters from a string. This string is passed into a function by reference (so a return type in not necessary). As an additional note, I am trying to keep this as efficient as possible. My original thinking was to simply shift this over to nested for loops, but my CSE prof maintains that a for loop with a string iterator inside is more efficient in some special cases, so I should stick with that. Please let me know if anyone can help!!
It seems to me the issue must lie in the remove function, as remove takes a const char *, not simply a char *. I assume that text.begin() is not const and therefore causes the issue. But if I make the the parameters of the function const string &text, we obviously cannot modify text by reference.
Here is the code I have so far:
in the main we have:
string temp = "~cool~";
XML * parser = new XML();
parser->clearSpecialChars(temp);
cout << temp;
and the function is:
void XML::clearSpecialChars(string &text)
{
char chars[]= ".,!()1234567890[]'<>:/{}_|=+;-`~";
for (unsigned int i = 0; i < 33; ++i)
{
text.erase(std::remove(text.begin(),text.end(),chars[i]),text.end());
}
}
however,I am getting a compiler error of:
XML.cpp: In member function ‘void XML::clearSpecialChars(std::string&)’:
XML.cpp:86:69: error: cannot convert ‘std::basic_string<char>::iterator {aka __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >}’
to ‘const char*’ for argument ‘1’ to ‘int remove(const char*)’
any thoughts would be greatly appreciated!
There are two functions called remove
. Here are the description to each of them: