C++ 什么';在C+中,std::mem#u fun和lambda之间的区别是什么+;11?
对于此小代码:C++ 什么';在C+中,std::mem#u fun和lambda之间的区别是什么+;11?,c++,c++11,lambda,C++,C++11,Lambda,对于此小代码: #include <thread> #include <iostream> #include <memory> #include <vector> #include <algorithm> using namespace std; void do_work(int id) { cout<<this_thread::get_id()<<" "<<id<<endl; }
#include <thread>
#include <iostream>
#include <memory>
#include <vector>
#include <algorithm>
using namespace std;
void do_work(int id)
{
cout<<this_thread::get_id()<<" "<<id<<endl;
}
int main()
{
vector<thread> threads;
for(int i = 0; i < 20; i++)
threads.push_back(thread(do_work, i));
for_each(begin(threads), end(threads), [](thread & t){
t.join();
});
}
备选案文2:
for_each(begin(threads), end(threads), mem_fn(&thread::join));
问题:
1.哪种用法更好?
2.mem\u fn
的用法是什么?你能简单地解释一下吗?你不明白的部分是什么?既然您已经演示了如何使用mem_-fun
,那么您在使用方面需要什么帮助?更好的标准是什么?你不明白哪一部分?既然您已经演示了如何使用mem_-fun
,那么您在使用方面需要什么帮助?更好的标准是什么?
for_each(begin(threads), end(threads), mem_fn(&thread::join));