にキャプチャするために失敗し、再現コードは、ここでラムダ
#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()
)
{
std::cout << "'y' is not present in 'v'\n";
} else
{
std::cout << "'y' is present in 'v'\n";
}
return 0;
}
である私は、私は選択肢の数を試してみましたが、thisとの数を読んだg++ -std=c++14 a.cpp -o a
でコンパイルし
error: invalid operands to binary expression
('X' and 'const (lambda at a.cpp:38:5)')
を取得しようSEの投稿にはError: variable “cannot be implicitly captured because no default capture mode has been specified”が含まれていますが、私は間違いを見ません。
に
std::find
を変更好む:をしてまで
std::find
を変更します。あなたはこのような何かを試すことができます!フォローアップの質問をすることができるのであれば、 'binary_search_if'のようなバイナリ検索のための' find_if'と同等のものがありますか? –
@ Remi.b - 私が知っている限り...いいえ:そうではありません。 – max66