![](/img/trans.png)
[英]Error] ISO C++ forbids comparison between pointer and integer [-fpermissive]
[英]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());
我一直在嘗試運行這段代碼,它在 lambda function 上給了我一個錯誤。
錯誤:ISO C++ 禁止指針和 integer [-fpermissive] 之間的比較
這在 HackerRank 中為名為split_strings()
的 function 工作。 誰能幫我解決它?
我的意圖是刪除以空格分隔的字符串中的所有空格,並返回一個指向已更改數組的最后一個元素的指針。
a
和b
是相同的類型,但" "
不是字符,它用於字符串文字。 考慮改成return a==b && a==' ';
你實際上有兩個問題:
第一個是您得到的錯誤,這是因為a==" "
將單個char
變量 ( a
) 與字符串文字進行比較。 要與單個空格進行比較,您需要與字符' '
進行比較。
第二個錯誤是您對newEnd
使用了錯誤的迭代器,或者您對比較 lambda 使用了錯誤的類型。
如果newstring
是std::string
則 lambda 是正確的,它應該使用兩個字符作為 arguments。 問題是std::unique
將返回一個std::string::iterator
。
如果newstring
是std::vector<string>
則 arguments 到 lambda 是std::string
值。 newEnd
迭代器是正確的。
最后,要刪除字符串中的所有空格,那么std::unique
似乎是相當糟糕的選擇(尤其是因為它應該刪除重復項)。
我寧願推薦std::erase
,如:
newstring.erase(std::erase(begin(newstring), end(newstring), ' '), end(newstring));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.