lambda表达式在while()中调用,为什么我们需要在lambda表达后添加()
lambda expression is called in the while(),Why do we need to add () after the lambda expression
while ([&s]()->bool {
cout << "Please Input you word<input "q " to exit>:";
return (cin >> s) && (s != "[enter image description here][1]q");
}())
lambda表达式是函数定义。调用函数时,必须使用括号,但是当您将函数传递为参数时,只需使用该名称。如果我们将lambda函数存储在称为"条件"的变量中,则可以更好地看到它:
#include <iostream>
#include <string>
using namespace std;
int main() {
string s{};
auto condition = [&s]() -> bool {
cout << "Please Input you word:";
return ((cin >> s) && (s != "enter image description here"));
};
while (condition()) {
}
return (0);
}
示例:http://cpp.sh/3k6js
参考:http://en.cppreference.com/w/cpp/language/lambda
这也可以写为:
#include <iostream>
#include <string>
int main() {
std::string s{};
while ([&s] {
std::cout << "Please Input you word:";
return ((std::cin >> s) && (s != "enter image description here"));
}()) {
}
return (0);
}
示例:http://cpp.sh/7v7gd
相关文章:
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 如何在C++中从两个单独的for循环中添加两个数组
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- POCO::PostgreSQL:如何将std::vector支持添加到`Binder::bind`
- 可组合的lambda/std::函数与std::可选
- 如何仅为一个函数添加延迟
- 如何防止 c++ 在从浮点型转换为双精度型(不适用于 IO)时添加额外的小数?
- 使用std::transform将一个范围的元素添加到另一个范围中
- 如何将更多文件夹添加到c++include路径
- 如何将元素添加到数组的线程安全函数?
- QT通过C++添加映射QML项目
- 如何将点击的信号和插槽添加到qt中的自定义按钮中
- C++Boost Asio Pool线程,带有lambda函数和传递引用变量
- lambda表达式在while()中调用,为什么我们需要在lambda表达后添加()
- 在C++中添加两个 lambda 函数
- C++ 添加到 lambda 的末尾
- 当databasereference :: runtransaction时,可以将捕获值添加到lambda中
- C++ - 使用 lambda 将映射添加到映射
- 从 LLVM 添加映射到C++ lambda
- 如何向lambda函数添加属性