好吧,我希望我在这里犯了一个愚蠢的错误.我有一个DisplayDevice3d列表,每个DisplayDevice3d都包含一个DisplayMode3d列表.我想删除DisplayDevice3d列表中没有任何DisplayMode3d的所有项目.我正在尝试使用Lambda来做它,即:
// If the device doesn't have any modes, remove it.
std::remove_if(MyDisplayDevices.begin(), MyDisplayDevices.end(),
[](DisplayDevice3d& device)
{
return device.Modes.size() == 0;
}
);
Run Code Online (Sandbox Code Playgroud)
即使出6名DisplayMode3d在MyDisplayDevices,只有1个有什么DisplayMode3d的其模式集合中,没有被从列表中删除.
我在这里犯了什么错误?
编辑:
好吧,我的错误是我应该使用MyDisplayDevices.remove_if而不是std :: remove_if,但是下面的答案对于使用std :: remove_if:p是正确的.
MyDisplayDevices.remove_if( [](DisplayDevice3d const & device)
{
return device.Modes.size() == 0;
});
Run Code Online (Sandbox Code Playgroud) 您好我在Windows文档中找到了此代码
但我没有得到什么意思
[&]
Run Code Online (Sandbox Code Playgroud)
只是请你能清楚我该怎么办?
它不是c ++标准的真实?
这是代码:
void parallel_matrix_multiply(double** m1, double** m2, double** result, size_t size)
{
parallel_for (size_t(0), size, [&](size_t i)
{
for (size_t j = 0; j < size; j++)
{
double temp = 0;
for (int k = 0; k < size; k++)
{
temp += m1[i][k] * m2[k][j];
}
result[i][j] = temp;
}
});
}
Run Code Online (Sandbox Code Playgroud) 我正在关注这个人关于 C++ Lambdas 的博客文章http://cpptruths.blogspot.com/2014/03/fun-with-lambdas-c14-style-part-1.html,在编译他的代码时,我遇到了编译器错误:
variable 'unit' cannot be implicitly captured in a lambda with no capture-default specified"
Run Code Online (Sandbox Code Playgroud)
它引用的行如下:
auto unit = [](auto x) {
return [=](){ return x; };
};
auto stringify = [](auto x) {
stringstream ss;
ss << x;
return unit(ss.str());
};
Run Code Online (Sandbox Code Playgroud)
这家伙似乎了解 C++ 中的新 Lambda 功能,而我当然不知道,这就是我现在在这里的原因。有人可以阐明这个问题吗?我需要做什么才能正确编译此代码?
提前致谢!:)
编辑:事实证明问题不在于单位或字符串化。让我粘贴新代码:
auto unit = [](auto x) {
return [=](){ return x; };
};
auto stringify = [unit](auto x) {
stringstream ss;
ss << x;
return unit(ss.str());
};
auto …Run Code Online (Sandbox Code Playgroud) 我第一次使用 lambda 并遇到了一个我无法解决的问题。
我收到多个错误:
<source>: In lambda function:
<source>:7:19: error: 'x' is not captured
7 | int sum = x + y;
| ^
<source>:6:20: note: the lambda has no capture-default
6 | auto lambda = []() {
| ^
<source>:4:9: note: 'int x' declared here
4 | int x = 5, y = 10;
| ^
<source>:7:23: error: 'y' is not captured
7 | int sum = x + y;
| ^
<source>:6:20: note: the lambda has no capture-default …Run Code Online (Sandbox Code Playgroud) 这是一个可重现的代码
#include <iostream>
#include <vector>
#include <algorithm>
class X
{
public:
int a;
X(int b)
: a(b)
{}
};
int main()
{
// Build the vector 'v'
std::vector<X> v;
X x1(7);
v.push_back(x1);
X x2(11);
v.push_back(x2);
X x3(16);
v.push_back(x3);
X x4(20);
v.push_back(x4);
X x5(22);
v.push_back(x5);
X x6(31);
v.push_back(x6);
X x7(38);
v.push_back(x7);
// Search if there is an element of 'v' which attribute 'a' equals 18
int y(18);
if (
std::find(
v.begin(),
v.end(),
[&](const X& o){return o.a == y;}
) == v.end() …Run Code Online (Sandbox Code Playgroud) 我正在尝试将名称转换为数字。我的问题是,当我尝试在 switch 语句中执行简单操作时,a=a+1我收到错误消息“在未指定捕获默认值的情况下,无法在 lambda 中隐式捕获变量 'a'”
在这里查找相同的错误消息,我发现我应该使用[],[=]或[&]。我的问题似乎更多是如何以及在哪里这样做。如果我去[](int a=0){};初始化变量的地方,那么我的消息是“错误:使用未声明的标识符‘a’”
这是我的问题的代码
#include <jni.h>
#include <string>
#include <iostream>
#include <iomanip>
#include <sstream>
using namespace std;
static int nameToNumber(string fn, string ln)
{
string nameOne = fn;
string nameTwo = ln;
[](int a=0){};
int b = 0;
int num = 0;
for_each(nameOne.begin(), nameOne.end(), [](char &c )
{
c=::toupper(c);
switch (c){
case 'A':
case 'J':
case 'S': a=a+1;
break;
case 'B':
case 'K':
case 'T': …Run Code Online (Sandbox Code Playgroud)