我只想知道如何在lambda表达式捕获括号内编写对。 因为下面的代码没有编译所以我错过了一些东西......
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
答案 0 :(得分: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]:
标识符是一个任意长的字母和数字序列。 [..]