Search code examples
c++listlambdaerase

remove upper letters from list and write it into another list


I must create a string list and remove upper letters with lambda and write it into another list, so i try but i have bug:[Error] request for member 'erase' in 'c', which is of non-class type 'const char'

list<string> ls {"hak","ala","alA","coS","KtOs"};
list<string> ls2;
for(auto k : ls)cout<<k<<endl<<endl;
transform(ls.begin(),ls.end(),ls.begin(),[](string &s) {    
             transform(s.begin(),s.end(),s.begin(), [](const char& c) { 
                  if(isupper(c) );return c.erase(isupper(c)) ; 
               });
          return s;

Solution

  • This lambda of yours:

    [](const char& c) { 
         if(isupper(c) );
              return c.erase(isupper(c)) ; 
    }
    

    You have a ; in your if... effectively translating it to a dead code... And c is of type char, it has no members. WHat are you even trying to do? isupper returns a non zero value (meaning it could be anything) to signify true.

    I presume what you want to do is:

    transform(ls.begin(), ls.end(), back_inserter(ls2),
                [](string s) -> string
                {
                    s.erase(remove_if(s.begin(), s.end(),
                               [](char c){
                                      return isupper(c);
                               }), s.end()
                           );
                        return s;
                 });
    

    Again, I used back_inserter for the new list because its assumed it doesn't contain any element yet. The other is Remove/Erase Idiom. And please see the documentation of isupper.

    Full example here: http://coliru.stacked-crooked.com/a/4df3c806d7d35de4