使用boost :: lambda_来压缩字符串中的空格

nav*_*tor 0 c++ stl boost-lambda

我使用boost :: lambda删除字符串中的后续空格,只留下一个空格.我试过这个程序.

#include <algorithm>
#include <iostream>
#include <string>
#include <boost/lambda/lambda.hpp>


int main()
{
    std::string s = "str     str st   st sss";
    //s.erase( std::unique(s.begin(), s.end(), (boost::lambda::_1 == ' ') && (boost::lambda::_2== ' ')), s.end()); ///< works
    s.erase( std::unique(s.begin(), s.end(), (boost::lambda::_1 == boost::lambda::_2== ' ')), s.end()); ///< does not work
    std::cout << s << std::endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

注释行工作正常,但未注释的行没有.

怎么

(boost::lambda::_1 == boost::lambda::_2== ' ') 
Run Code Online (Sandbox Code Playgroud)

不同于

(boost::lambda::_1 == ' ') && (boost::lambda::_2== ' '))
Run Code Online (Sandbox Code Playgroud)

在上面的程序.评论的那个也给了我一个警告,"警告C4805:'==':'bool'类型的不安全混合,并在操作中输入'const char'"

谢谢.

rob*_*.14 5

在C和C++中,a == b == x与(a == x)&&(b == x)非常不同,前者被解释为(a == b)== x,它将a与b进行比较将该比较的结果(真或假)与x进行比较.在你的情况下,x是一个空格字符,在使用ASCII的典型实现中,它的代码等于32,将它与boolean值进行比较,boolean值转换为0或1总是为false.