参考资料:
http://blog.csdn.net/qq575787460/article/details/8531397
http://blog.csdn.net/crayondeng/article/details/18563121
1.Lambda表达式
#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>
class TestLambda{
int data;
public:
TestLambda() :data(10){}
TestLambda(int d) :data(d){}
void Test(){
vector<int> vi;
vi.push_back(1);
vi.push_back(2);
vi.push_back(3);
int count = 5;
//无函数对象参数。int v 表示传入该lambda表达式的参数
for_each(vi.begin(), vi.end(), [](int v){cout << v << endl; });
//以值方式传递作用域内所有可见的局部变量(包括this),即当前lambda表达式所在作用域范围内所有可见的局部变量
//值传递方式,count局部变量
for_each(vi.begin(), vi.end(), [=](int v){cout << v + count << endl; });
//值传递方式,类的成员变量data
for_each(vi.begin(), vi.end(), [=](int v){cout << v + data << endl; });
//引用传递
for_each(vi.begin(), vi.end(), [&](int v){cout << v + count << endl; count++; });
for_each(vi.begin(), vi.end(), [&](int v){cout << v + data << endl; data++; });
cout << "count = " << count << endl;
cout << "data = " << data << endl;
count = 5;
data = 10;
//传递局部变量count,而且使用了mutable关键字,表示可以修改值传递过来的count变量
for_each(vi.begin(), vi.end(), [count](int v)mutable{cout << v + count << endl; count++; });
//当然啦,上面终究是值传递,这里的count的值依然是5
cout << "count=" << count << endl;
}
};
int main()
{
TestLambda lam;
lam.Test();
//system("pause");
return 0;
}
2.auto和decltype关键字
#include <iostream>
using namespace std;
#include <map>
#include <string>
//auto作为占位符,具体的类型由t1+t2的值的类型来决定
template <typename T1, typename T2>
auto multiply(T1 t1, T2 t2)->decltype(t1 + t2)
{
return t1*t2;
}
int main()
{
map<string, int> mperson;
mperson.insert(make_pair("kobe", 35));
mperson.insert(make_pair("jordan", 45));
//用 auto 替代 map<string, int>::iterator
for (map<string, int>::iterator iter = mperson.begin(); iter != mperson.end(); iter++){
cout << (*iter).first << "'s age is " << (*iter).second << endl;
}
int a = 10;
float c = 0.5;
auto result = multiply(a, c);
cout << result << endl;
//system("pause");
return 0;
}
3.std::function
#include <iostream>
using namespace std;
#include <functional>
#include <string>
int MaxNumber(int a, int b){
return (a > b ? a : b);
}
class Person{
string name;
int age;
public:
Person(string n, int a) :name(n), age(a){}
void show()const{
cout << name << " , " << age << endl;
}
};
int main()
{
//普通函数
function<int(int, int)> myfunc1;
myfunc1 = MaxNumber;
cout << myfunc1(5, 6) << endl;
//lambda
function<int(int, int)> myfunc2 = [](int a, int b){return a > b ? a : b; };
cout << myfunc2(17, 12) << endl;
//类的非静态成员函数,要用bind
Person p1("Kobe", 35);
function<void()> myfunc3 = bind(&Person::show, p1);
myfunc3();
//system("pause");
return 0;
}
4.std::bind
#include <iostream>
using namespace std;
#include <functional>
void func1(int x, int y, int z){
cout << x << " , " << y << " , " << z << endl;
}
void func2(int &a, int &b){
a++;
b++;
cout << a << " , " << b << endl;
}
class Function{
public:
void func3(int m, int n){
cout << m << " , " << n << endl;
}
static void func4(int m, int n){
cout << m << " , " << n << endl;
}
};
int main()
{
function<void()> f1 = bind(func1, 11, 22, 33);
f1();
function<void(int, int)> f2 = bind(func1, placeholders::_1, placeholders::_2, 33);
f2(11,22);
function<void(int, int)> f3 = bind(func1, placeholders::_2, placeholders::_1, 33);
f3(22, 11);
int n = 2;
int m = 3;
function<void(int)> f4 = bind(func2, n, placeholders::_1);
f4(m);
cout << n << endl;
cout << m << endl;
Function ffun;
//类的非静态成员函数,要调用ffun对象,而且记得要用取地址符&
auto f5 = bind(&Function::func3, ffun, placeholders::_1, placeholders::_2);
f5(10, 20);
//静态函数,可以不用bind
function<void(int, int)> f6 = &Function::func4;
f6(20, 30);
//静态函数,这里不需要ffun对象
function<void(int)> f7 = bind(&Function::func4, 10, placeholders::_1);
f7(50);
//system("pause");
return 0;
}
5.整数0、NULL、nullptr
#include <iostream>
using namespace std;
//重载函数fun
void fun(int num){
cout << "fun-->int" << endl;
}
void fun(char *ch){
cout << "fun-->char*" << endl;
}
void test(int num){
}
int main()
{
fun(0); //数值0,为int。输出fun-->int
fun(NULL); //也是数值0,为int。输出fun-->int
fun(nullptr); //空指针,为char*。输出fun-->char*
//其实,NULL就是一个宏定义,为整数0的宏定义。而nullptr是一个内定的常量,是一个表示空指针的标识
//nullptr不是一个整数,比如下面的例子
//test(nullptr); //编译错误。
test(NULL); //正确。
//system("pause");
return 0;
}