- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
考虑下面的模板函数 sort(...)。这是一个围绕 std::sort 的包装函数。目的是在对用户定义类的 vector 进行排序时提供更好的语法。第一个参数是要排序的 vector 。第二个参数是一个函数,指定如何对 vector 进行排序(在哪个公共(public)成员变量或函数上)。
std::sort 需要一个比较函数来对 vector 中的不同项目进行排序。这在我的排序函数中被声明为 lambda。但是,此代码仅在 lambda 声明为“auto”时编译,而不是在声明为 bool 时编译。我觉得这很奇怪。有人可以解释一下吗?
(代码应该按现在的样子编译。要观察问题,请取消注释以“bool”开头的行)。
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
// Copy function is needed in sort(...), below
template<typename T>
auto copy(const std::vector<T>& v)
{
auto r = std::vector<T>();
r.reserve(v.size());
for(const auto & e : v) {
r.push_back(e);
}
return std::move(r);
}
template<typename T, typename F>
auto sort(const std::vector<T>& v, const F& f)
{
auto r = copy(v);
// ///////////// Consider the following line: /////////////////
// bool compare = [=](const T& lhs, const T& rhs) // does not compile, why?
// auto compare = [=](const T& lhs, const T& rhs) -> bool // compiles
auto compare = [=](const T& lhs, const T& rhs) // compiles
{
return f(lhs) < f(rhs);
};
std::sort(r.begin(), r.end(), compare);
return std::move(r);
}
struct Foo {
int id;
string message;
};
int main()
{
auto unordered = vector<Foo>();
unordered.push_back(Foo{2, "the last element"});
unordered.push_back(Foo{1, "the first element"});
// I want to sort the vector by 'id'.
auto ordered = sort(unordered, [](const auto& e) { return e.id; });
cout << "Ordered vector (by id):" << endl;
for(const auto& e : ordered) {
cout << e.id << ", " << e.message << endl;
}
return 0;
}
最佳答案
lambda 的类型是实现定义的。因此,如果您要声明一个变量来保存 lambda,它必须始终是 auto
类型。您似乎将 lambda 的返回类型与实际保存 lambda 本身的变量类型混淆了。
// variable type | variable identifier | lambda expression (return type deduced)
bool compare = [=](const T& lhs, const T& rhs)
// variable type | variable identifier | lambda expression | return type
auto compare = [=](const T& lhs, const T& rhs) -> bool
// variable type | variable identifier | lambda expression (return type deduced)
auto compare = [=](const T& lhs, const T& rhs)
上表说明了声明的每个部分所涉及的内容。在你的代码中 compare
是一个 lamda。如果您声明一个 bool
类型的变量,然后尝试为其分配一个 lamda,您将不可避免地遇到编译器错误。
关于c++ - Lamda 仅在声明为 'auto' 时编译,而不是在声明为 'bool' 时编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51235135/
zeta 在 critic 方法中代表什么?我相信它会跟踪状态- Action 对并表示资格跟踪,这是状态- Action 的临时记录,但 zeta 究竟代表什么以及它在 C++ 中的外观如何(例如
我想列出一个方法调用的所有方法。 void create() throws MyException { System.out.println("TEST"); of("String")
所以我只是在玩 kotlin,无法解决这个问题。 fun itsAfunction() = 10 fun check(function:()->Int):Int{ re
想知道是否可以让网络应用程序将文件 (userid.input.json) 上传到 Amazon S3,这会触发一个 lambda 函数来读取文件、进行一些处理并保存结果作为另一个 (userid.o
假设我有一个包含 Button 和 Spinbox 的 Widget。单击按钮时,我希望发出 Spinbox 的值。 我看到了两种可能的方法: 要么我可以创建一个私有(private)成员函数 //.
java 8 lamda Stream的Collectors.toMap 参数 使用toMap()函数之后,返回的就是一个Map了,自然会需要key和value。 toMap()的第一个参数就是用来生
fun theItemDTO.toDomainModel( domainOrderId: String, pIds: List = emptyList() ): theItem = l
我有一个 android 项目,它依赖于一个库模块。我的模块中有一个这样的接口(interface): public interface SimpleAnimationListener exte
我有getter方法 @JsonInclude(Include.NON_NULL) public Date getVerifiedFrom() { if(invoices == null ||
假设,我有一个卷积层的 10x10x4 中间输出,我需要将其分成 100 个 1x1x4 卷,并对每个卷应用 softmax,以获得 100 个输出网络。有没有办法在不使用 Lambda 层的情况下完
我写了这样的 JavaFX 摆弄: MenuItem menuItem1 = new MenuItem("Item 1") menuItem1.setOnAction(e -> {
我在研究 lambda 表达式时遇到了这个问题: #include using std::cout; int main() { auto lam = [](int a){ cout #inc
考虑下面的模板函数 sort(...)。这是一个围绕 std::sort 的包装函数。目的是在对用户定义类的 vector 进行排序时提供更好的语法。第一个参数是要排序的 vector 。第二个参数是
TLDR header authorization 没有随 apollo 一起发送。这导致 You do not have the appropriate capabilities to perfor
我在 API 网关之后使用 NodeJs Lambda。 var AWS = require('aws-sdk'); const cognito = new AWS.CognitoIdentitySe
我每天晚上 8 点使用 chalice 和 lambda 函数安排 ec2 实例关闭。 我已经配置了 chalice ,但无法使用 chalice 触发或集成 python 脚本 导入 boto3 #
我正在学习 Java 8 中的 Lambda 表达式和方法引用,发现我们可以通过使用“super”来引用方法的父类(super class)版本,如: super ::名称 但是当我这样做时,它不起作
如何将 CommaDelimitedList 参数转换为字符串,以便将其作为环境变量传递给 Lamda 函数? 下面是示例 CommaDelimitedList 参数,其中包含 AWS 区域列表,我需
我使用的第三方 API 返回一个类型为 int[] 的数组。我们将其称为 readWords: int[] readWords 然而,这个重新调整的数组应该是一个 uint[]。所以我正在进行以下转换
我是一名优秀的程序员,十分优秀!