Vue框架:
从项目学Vue
OJ算法系列:
神机百炼 - 算法详解
Linux操作系统:
风后奇门 - linux
//1, 函数:
void fmin(int x, int y){
return x<y?x:y;
}
//2,函数指针:
void func(int, int) = fmin();
func(1, 2);
//3,仿函数对象:
struct IntLess{
bool operator()(int &x, int &y){
return x<y?x:y;
}
}
IntLess(1, 2);
//4,简易lambda表达式:
auto function = [](int x, int y){
return x<y?x:y;
};
function(1, 2);
//附加说明的五部分:附加关键词一般为mutable、noexcept
auto func = [](int x, int y)mutable->int{
return x<y?x:y;
}
//完整的四部分:
auto func1 = [](int x, int y)->int{
return x<y?x:y;
};
//省略返回类型,可自动推导返回类型
auto func2 = [](int x, int y){
return x<y?x:y;
};
//当不需要参数时候,可省略参数
auto func3 = []{
cout<< "hello world"<< endl;
};
//报错:常量不可修改
int main(){
int x = 0;
auto func = [=](){
x++;
cout<<x<<endl;
};
cout<<x<<endl;
}
//()mutable
int main(){
int x = 0;
auto func = [=]()mutable{
x++;
cout<<x<<endl;
};
cout<<x<<endl;
}
auto func = [](int x, int y){
return x<y?x:y;
};
cout<< decltype(func).name();
decltype和auto的区别:
1. decltype用于需要声明变量类型的情况,如函数形参
2. auto用于需要不声明变量类型的情况,如单纯使用
decltype使用:
auto func1 = []{cout<<"极简lambda"<<endl;};
auto func2 = func1;
decltype(func1) func3 = func1;
lambda表达式相对函数/函数指针/仿函数对象来说写作更快捷,
适用于编写依据类的不同字段,对类的对象排序,类似这样的代码
下面给出一个商品类,求分别按照商品价格/评分/名称,对商品对象进行排序:
商品类:
class Goods{
private:
int price;
double evaluate;
char* name;
public:
//八大默认函数,自动补全。
Goods(int _price, double _evaluete, char* _name){
price = _price;
evaluete = _evaluete;
name = _name;
}
};
vector<Goods> v{{1, 1.0, "a"}, {2, 2.0, "b"}, {3, 3.0, "c"}, {4, 4.0, "d"}};
sort(v.begin(), v.end(), [](Goods &a, Goods &b){
return a.price > b.price;
});
sort(v.begin(), v.end(), [](Goods &a, Goods &b){
return a.price < b.price;
});
sort(v.begin(), v.end(), [](Goods &a, Goods &b){
return a.evaluete > b.evaluete;
});
sort(v.begin(), v.end(), [](Goods &a, Goods &b){
return a.evaluete < b.evaluete;
});
sort(v.begin(), v.end(), [](Goods &a, Goods &b){
return strcmp(a.name, b.name);
});
sort(v.begin(), v.end(), [](Goods &a, Goods &b){
return -1*strcmp(a.name, b.name);
});
bool ComparePriceLess(Goods &a, Goods &b){
return a.price < b.price;
}
bool ComparePriceMore(Goods &a, Goods &b){
return a.price < b.price;
}
bool CompareEvaluateLess(Goods &a, Goods &b){
return a.price < b.price;
}
bool CompareEvaluateMore(Goods &a, Goods &b){
return a.price < b.price;
}
bool CompareNameLess(Goods &a, Goods &b){
return a.price < b.price;
}
bool CompareNameMore(Goods &a, Goods &b){
return a.price < b.price;
}
vector<Goods> v{{1, 1.0, "a"}, {2, 2.0, "b"}, {3, 3.0, "c"}, {4, 4.0, "d"}};
sort(v.begin(), v.end(), ComparePriceLess);
sort(v.begin(), v.end(), ComparePriceMore);
sort(v.begin(), v.end(), CompareEvaluateLess);
sort(v.begin(), v.end(), CompareEvaluateMore);
sort(v.begin(), v.end(), CompareNameLess);
sort(v.begin(), v.end(), CompareNameMore);
}
struct ComparePriceLess{
bool operator()(Goods &a, Goods &b){
return a.price < b.price;
}
};
struct ComparePriceMore){
bool operator()(Goods &a, Goods &b){
return a.price > b.price;
}
};
struct CompareEvaluateLess{
bool operator()(Goods &a, Goods &b){
return a.evaluate < b.evaluate;
}
};
struct CompareEvaluateLess{
bool operator()(Goods &a, Goods &b){
return a.evaluate > b.evaluate;
}
};
struct CompareNameLess{
bool operator()(Goods &a, Goods &b){
return -1*strcmp(a.name, b.name);
}
};
struct CompareNameLess{
bool operator()(Goods &a, Goods &b){
return strcmp(a.name, b.name);
}
};
vector<Goods> v{{1, 1.0, "a"}, {2, 2.0, "b"}, {3, 3.0, "c"}, {4, 4.0, "d"}};
sort(v.begin(), v.end(), ComparePriceLess());
sort(v.begin(), v.end(), ComparePriceMore());
sort(v.begin(), v.end(), CompareEvaluateLess());
sort(v.begin(), v.end(), CompareEvaluateMore());
sort(v.begin(), v.end(), CompareNameLess());
sort(v.begin(), v.end(), CompareNameMore());
};