3

ラムダ式のキャプチャ ブラケット内にペアを記述する方法を知りたいだけです。次のコードはコンパイルされないため、何か不足しています...

std::vector<std::pair<std::string, std::string>> container1_;

for( auto iter : container1_ )
{
    auto result = std::find_if( container2_.cbegin(), container2_.cend(),
        [iter.first]( const std::string& str )->bool { return str == iter.first; } );
}

In member function ‘bool MsgChecker::CheckKeys()’:
error: expected ‘,’ before ‘.’ token
error: expected identifier before ‘.’ token
4

1 に答える 1

8
  [iter.first]( const std::string& str )->bool { return str == iter.first; }
// ^^^^^^^^^^

Lambda キャプチャは識別子用であり、任意の式やその他のもの用ではありません。

渡すだけですiter

  [iter]( const std::string& str )->bool { return str == iter.first; }

[C++11: 5.1.2/1]:

[..]

 キャプチャ:
   識別子
   & 識別子
   this

[C++11: 2.11/1]:識別子は、文字と数字の任意の長さのシーケンスです。[..]

于 2013-11-05T13:50:18.710 に答える