在C ++ 11中std :: mem_fun和lambda之间的区别是什么?

时间:2016-12-15 04:15:30

标签: c++ c++11 lambda

对于这个小代码:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<layer-list>
        <item android:left="1dp" android:top="3dp">
            <shape>
                <solid android:color="#a5040d" />
                <corners android:radius="3dip"/>
            </shape>
        </item>
    </layer-list>
  </item>
<item>
    <layer-list>
        <item android:left="0dp" android:top="0dp">
            <shape>
                    <solid android:color="#99080d" />
                <corners android:radius="3dip"/>
            </shape>
        </item>
        <item android:bottom="3dp" android:right="2dp">
            <shape>
                <solid android:color="#a5040d" />
                <corners android:radius="3dip"/>
            </shape>
        </item>
    </layer-list>
</item>

选项1:

#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), [](thread & t){
                t.join();
            });

问题: 1.哪种用法更好? 2. for_each(begin(threads), end(threads), mem_fn(&thread::join)); 的用法是什么?你能给出一个简单的解释吗?

0 个答案:

没有答案