什么意思是“[](unsigned char x)”? [重复]

问题描述 投票:0回答:1

这个问题在这里已有答案:

什么意思是[](unsigned char x)

这是我的代码:

#include <algorithm>

std::string str1 = "Text with some     spaces";

str1.erase(std::remove(str1.begin(), str1.end(), ' '), str1.end());

std::cout << str1 << '\n';

std::string str2 = "Text\n with\tsome \t whitesspaces\n\n";

str2.erase(std::remove_if(str2.begin(), str2.end(), [](unsigned char x) {return std::isspace(x);}), str2.end());

std::cout << str2 <<'\n';
c++ char unsigned
1个回答
0
投票

它是lambda函数定义的开始:

[](unsigned char x) {
    return std::isspace(x);
}

这定义了一个临时函数接收unsigned char并返回int(由std::isspace的返回值自动确定,因为lambda没有指定返回类型)。

© www.soinside.com 2019 - 2024. All rights reserved.