匿名函數在vc中的使用

看到js中的匿名函數,總以爲很炫,一查,這個是業界標準,php、java、c++都已經支持,連忙查下網絡學習下,查到這篇寫的不錯,介紹了lambda expression在c++中的調用php

http://www.cprogramming.com/c++11/c++11-lambda-closures.htmlhtml

參考它,我寫了測試代碼練下。java


#include "stdafx.h"ios

#include <iostream>c++

#include <string>express

#include <map>網絡

#include <algorithm>函數

#include <functional>學習

using namespace std;測試



void testCase1()

{

map<int, string> datas;

datas[0] = "zero";

datas[1] = "one";

datas[2] = "two";

for_each(datas.begin(), datas.end(), [](pair<int, string> pair){

cout << pair.first << "\t" << pair.second << endl;

});

}

void testCase2()

{

map<int, string> datas;

datas[0] = "zero";

datas[1] = "one";

datas[2] = "two";

for(auto pos = datas.begin(); pos != datas.end(); pos++)

{

cout << pos->first << "\t" << pos->second << endl;

}

}

void testCase3()

{

std::function<void (pair<int, string>) > output;

map<int, string> datas;

datas[0] = "zero";

datas[1] = "one";

datas[2] = "two";


/*

output = [](pair<int, string> _pair){

cout << _pair.first << "\t" << _pair.second << endl;

cout << datas.size() << endl;

};

*/

output = [&](pair<int, string> _pair){

cout << _pair.first << "\t" << _pair.second << endl;

cout << datas.size() << endl;

};

if(output)

{

for_each(datas.begin(), datas.end(), output);

}

}


int _tmain(int argc, _TCHAR* argv[])

{

string input;

testCase1();

testCase2();

testCase3();

cin>>input;


return 0;

}

相關文章
相關標籤/搜索