【发布时间】:2022-01-20 18:01:45
【问题描述】:
我正在尝试使用 lambda 表达式对 measure 类的数组 m 进行排序,如下所示:
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <math.h>
using namespace std;
struct measure{
int day;
int cow;
int change;
};
int main()
{
int N;
cin >> N;
measure m[N];
for (int i = 0; i < N; i++){
measure m_i;
cin >> m_i.day >> m_i.cow >> m_i.change;
m[i] = m_i;
}
sort(m, m + N, [](measure a, measure b) {return a.day < b.day;});
}
但是,尝试在 VS Code(使用 C++17)中构建任务时出现错误:
错误:预期的表达式
sort(m, m + N, [](measure a, measure b) {return a.day < b.day;}); ^生成 1 个错误。
构建完成但出现错误。
我已经在其他编译器上毫无困难地测试了这段代码。为什么 VS Code 会出现这个错误?
【问题讨论】:
-
无法重现。你用的是 C++98 编译器吗?
-
那么您可能需要在编译器中启用 C++11 或更高版本。使用“打开 C++11
”搜索谷歌,这应该可以满足您的需求。另外,如果您像我怀疑的那样使用 g++,请将 -pedantic-errors添加到您的编译命令中,这样它就会在您声明的 VLA 上出错。将该数组更改为std::vector。 -
不要用你自己的话重述编译器信息。剪切和粘贴,完整且未经编辑。
-
修复了 N 的问题
-
“我正在使用 C++17 编译器”no you are not