3

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!

MS-DDOS
  • 578
  • 5
  • 15
  • 2
    Have you included the ``algorithm`` header? There's another function called ``remove`` and that one accepts ``const char*`` as a parameter. – hinafu Nov 24 '12 at 02:56

2 Answers2

3

There are two functions called remove. Here are the description to each of them:

http://www.cplusplus.com/reference/algorithm/remove/

http://www.cplusplus.com/reference/clibrary/cstdio/remove/

hinafu
  • 689
  • 2
  • 5
  • 13
  • 1
    and that'll do it - I only included the stl. Awful when you realize you missed something so stupid. Thanks so much for paying attention! – MS-DDOS Nov 24 '12 at 02:59
1

The hard-coded "33" makes me cringe. Did you really want '\x00' to be included in your set of special characters?

Modern C++ offloads loops to library routines if possible. Here is a different implementation.

void XML::clearSpecialChars(string &text)
{
    const string chars = ".,!()1234567890[]'<>:/{}_|=+;-`~\x00";

    auto new_end = std::remove_if(text.begin(), text.end(), 
        [chars](string::value_type c)
            { return chars.find(c) != string::npos; });
    text.erase(new_end, text.end());
}
user515430
  • 3,341
  • 2
  • 17
  • 13
  • Well we had an strlen there earlier and decided because the special chars don't change that we could save space by not including `` and hardcoding 33 instead. It looks like iterators are becoming more and more popular as the day goes by, can you recommend any good reading that could help me get up to speed? – MS-DDOS Nov 24 '12 at 19:29
  • "Generic Programming and the STL" by Matthew Austern is the best source I know. – user515430 Nov 26 '12 at 01:29