Milton C
Milton C

Reputation: 123

Remove char if infront of word in vector of string

I have a vector of strings with different chars infront of them. for example:

"Hello
(Hello

I want to remove the first occurrence of the char. So if there is a " or ( before the word, I want it gone. My code so far is this.

void wash(std::vector<std::string> & data)
{
    std::string b_chars = "\"'("; //Before_chars
    std::string a_chars = "!?;,:.\"')"; //after_chars

    data.erase(std::remove_if(data.begin(), data.end(), [&b_chars](const char& c) {
        return data.find_first_of(b_chars) != std::string::npos;
    }), data.end());  
}

Upvotes: 3

Views: 52

Answers (3)

molbdnilo
molbdnilo

Reputation: 66371

Your condition is wrong - you should determine whether c is one of the offending characters, which is b_chars.find(c) != std::string::npos.

To iterate over the entire vector, you could go:

std::for_each(data.begin(),
              data.end(),
              [&b_chars](std::string& str)
              {
                  str.erase(std::remove_if(str.begin(), 
                              str.end(), 
                              [&b_chars](const char& c) 
                                  {return b_chars.find(c) != std::string::npos;}),
               data.end());
              });
          }

But it makes sense to have a separate string-washing function and not limit yourself to vectors of strings (I didn't read your code properly because this is a more useful building block to start with):

void wash_string(std::string & s)
{
    static const std::string b_chars = "\"'("; //Before_chars
    static const std::string a_chars = "!?;,:.\"')"; //after_chars

    s.erase(std::remove_if(s.begin(), 
                           s.end(), 
                          [&b_chars](const char& c) 
                             {return b_chars.find(c) != std::string::npos;}), 
               s.end());  
}

void wash(std::vector<std::string> & data)
{
    std::for_each(data.begin(), data.end(), wash_string);
}

Upvotes: 2

Maf
Maf

Reputation: 725

If you want to remove these the chars only before the first occurrence of a 'normal char':

for (vector<std::string>::iterator vt_it = data.begin(); vt_it<data.end(); ++vt_it)
    {
        std::string::iterator str_it = (*vt_it).begin();
        while (str_it != (*vt_it).end())
        {
            if ((b_chars).find((*str_it)) == std::string::npos)
                break;
            str_it++;
        }

        (*vt_it).erase ((*vt_it).begin(), str_it); 
    }

But if you want to remove all those chars:

for (vector<std::string>::iterator vt_it = data.begin(); vt_it<data.end(); ++vt_it)
    {
        (*vt_it).erase(
            std::remove_if(
                (*vt_it).begin(), 
                (*vt_it).end(), 
                [&b_chars](const char& c) {return b_chars.find(c) != std::string::npos;}),
            (*vt_it).end()
        );
    } 

OBS. You didn't reffer anything about what you want to do with a_chars. I tested all the codes in these post, included mine.

Upvotes: 0

marcinj
marcinj

Reputation: 49976

There are various problems with your code:

  1. in you code you dont remove chars from a string but from a vector of string. You should iterate your vector and do the removal on each string.
  2. As in molbdnilo answer, you need to change the condition of your lambda to find character inside the string of offending chars

    void wash(std::vector<std::string> & data)
    {
       std::string b_chars = "\"'("; //Before_chars
       std::string a_chars = "!?;,:.\"')"; //after_chars
    
       for (auto& str : data) {
           str.erase(std::remove_if(str.begin(), str.end(),
                                  [&](const char &c) { return b_chars.find_first_of(c) != std::string::npos; }),
                   str.end());
       }
    }
    

Upvotes: 0

Related Questions