我只想知道如何在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
[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]:
标识符是字母和数字的任意长序列。[..]
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句