Ajay Bhargav
Ajay Bhargav

Reputation: 1

error: ISO C++ forbids comparison between pointer and integer [-fpermissive] in a Lambda function

vector<string>::iterator newEnd=unique(newstring.begin(),newstring.end(),[](const char &a,const char &b){return a==b && a==" "; });
newstring.erase(newEnd,newstring.end());

I've been trying to run this piece of code and it's giving me an error on the lambda function.

error: ISO C++ forbids comparison between pointer and integer [-fpermissive]

This was working in HackerRank for a function called split_strings(). Can anyone please help me solve it?

My intention is to remove all the spaces in a space-separated string, and return a pointer to the last element of the changed array.

Upvotes: 0

Views: 644

Answers (2)

DynasticSponge
DynasticSponge

Reputation: 1441

a and b are the same type, but " " is not a character, it is used for string literals. Consider changing to return a==b && a==' ';

Upvotes: 2

Some programmer dude
Some programmer dude

Reputation: 409166

You actually have two problems:

  1. The first is the error you get, which is because of a==" ", where you compare a single char variable (a) with a string literal. To compare to a single space you need to compare to the character ' '.

  2. The second error is either that you use the wrong iterator for newEnd or that you use the wrong types for the comparison lambdas.

    If newstring is a std::string then the lambda is correct, it should take two characters as arguments. The problem is that then std::unique will return a std::string::iterator.

    If newstring is a std::vector<string> then the arguments to the lambda are std::string values. The newEnd iterator will be correct though.


Lastly, to remove all spaces in a string then std::unique seems rather bad choice (especially since it's supposed to remove duplicates).

I would rather recommend std::erase, as in:

newstring.erase(std::erase(begin(newstring), end(newstring), ' '), end(newstring));

Upvotes: 1

Related Questions