开发者

Boost lambda::_1 in C++ 0x

开发者 https://www.devze.com 2023-03-31 19:59 出处:网络
int main() { std::vector<int> v; v.push_back(1); v.push_back(3); v.push_back(2); std::for_each(v.begin(), v.end(), std::cout << boost::lambda::_1 << \"\\n\");开发者_高级运维
int main() 
{ 
    std::vector<int> v; 
    v.push_back(1); 
    v.push_back(3); 
    v.push_back(2); 

    std::for_each(v.begin(), v.end(), std::cout << boost::lambda::_1 << "\n");开发者_高级运维
} 

Can this code be translated to C++ without using Boost? I know C++ 0x lambda expression syntax, but didn't try to use placeholders in such context.


No placeholder needed in this case, as lambdas capture the parameter:

std::for_each(v.begin(), v.end(), [](int x){std::cout << x << "\n";});
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号