Reputation: 1
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
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
Reputation: 409166
You actually have two problems:
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 ' '
.
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