// 这里要求n>=0,同时n的取值不能太大,会溢出
// 为了方便,这里并没有处理上面说到的问题
int factorial(int n) {
int fact = 1;
for (int i = 1; i <= n; ++ i) fact *= i;
return fact;
}
autofactorial = [](int n) {
int fact = 1;
for (int i = 1; i <= n; ++ i) fact *= i;
return fact;
};
#include <iostream>
#include <functional>
std::function<int(int)> getFactorialFunc(int n) {
return [n](int x) {
int fact = 1;
for (; x >= 1; x -= n) fact *= x;
return fact;
};
}
int main() {
// 构造要求的三个函数
autofactorial1 = getFactorialFunc(1);
autofactorial2 = getFactorialFunc(2);
autofactorial3 = getFactorialFunc(3);
// 调用
std::cout << factorial1(10) << std::endl;
std::cout << factorial2(10) << std::endl;
std::cout << factorial3(10) << std::endl;
}
g++ factorial_lambda.cpp -o factorial_lambda.out --std=c++11
#include <iostream>
#include <functional>
// 两个参数的阶乘
int factorial(int n, int step) {
int r = 1;
for (; n >= 1; n -= step) {
r *= n;
}
return r;
}
// curring化的阶乘
std::function<int(int)> currying_factorial(int step) {
return [step](int n) {
return factorial(n, step);
};
}
int main() {
// 调用普通函数
std::cout << factorial(10, 1) << std::endl;
std::cout << factorial(10, 2) << std::endl;
std::cout << factorial(10, 3) << std::endl;
// 调用currying函数
std::cout << currying_factorial(1)(10) << std::endl;
std::cout << currying_factorial(2)(10) << std::endl;
std::cout << currying_factorial(3)(10) << std::endl;
return 0;
}
#include <iostream>
#include <functional>
int operate(int x, int y, const std::function<int(int, int)> &op) {
return op(x, y);
}
int main() {
autoadd = [](int x, int y) { return x + y;};
automul = [](int x, int y) { return x - y;};
std::cout << operate(10, 5, add) << std::endl;
std::cout << operate(10, 5, mul) << std::endl;
return 0;
}
#include <algorithm>
#include <cmath>
void abssort(float* x, unsigned n) {
std::sort(x, x + n,
// Lambda expression begins
[](float a, float b) {
return (std::abs(a) < std::abs(b));
} // end of lambda expression
);
}
struct S { void f(int i); };
void S::f(int i) {
[&, i]{}; // OK
[=, &i]{}; // OK
[&, &i]{}; // ERROR: i preceded by & when & is the default
[=, this]{}; // ERROR: this when = is the default
[i, i]{}; // ERROR: i repeated
}
template<class... Args>
void f(Args... args) {
auto x = [args...] { return g(args...); };
x();
}
pNums = make_unique<vector<int>>(nums);
//...
auto a = [ptr = move(pNums)]()
{
// use ptr
};
autoadd = [] (int first, int second)
{
return first + second;
};
autoadd = [] (autofirst, autosecond)
{
return first + second;
};
#include <iostream>
int main()
{
int n = 10;
autolambda1 = [n](int x) {
/* ++ n; */ // 这句编译会出错,错误信息如下:
// error: cannot assign to a variable captured
// by copy in a non-mutable lambda
return x + n;
};
autolambda2 = [n](int x) mutable {
++ n;
return x + n;
};
std::cout << lambda1(5) << " " << n << std::endl;
std::cout << lambda2(5) << " " << n << std::endl;
return 0;
}
// throw_lambda_expression.cpp
// compile with: /W4 /EHsc
int main() // C4297 expected
{
[]() throw() { throw 5; }();
}
#include <iostream>
#include <typeinfo>
int main() {
autolambda1 = [](int i) {return i;};
autolambda2 = [](int i) -> bool {return i;};
autolambda3 = [](int i) -> float {return i;};
/* auto lambda4 = []{ return {1, 2}; };*/ // ERROR: return type is void
// cannot deduce lambda return type
autox1 = lambda1(10);
autox2 = lambda2(10);
autox3 = lambda3(10);
std::cout << x1 << " " << typeid(x1).name() << std::endl;
std::cout << x2 << " " << typeid(x2).name() << std::endl;
std::cout << x3 << " " << typeid(x3).name() << std::endl;
return 0;
}
// captures_lambda_expression.cpp
// compile with: /W4 /EHsc
#include <iostream>
using namespace std;
int main()
{
int m = 0;
int n = 0;
[&, n] (int a) mutable { m = ++n + a; }(4);
cout << m << endl << n << endl;
}
void fillVector(vector<int>& v)
{
// A local static variable.
static int nextValue = 1;
// The lambda expression that appears in the following call to
// the generate function modifies and uses the local static
// variable nextValue.
generate(v.begin(), v.end(), [] { return nextValue++; });
//WARNING: this is not thread-safe and is shown for illustration only
}
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector< pair<int, int> > arr;
arr.push_back(make_pair(1, 4));
arr.push_back(make_pair(2, 3));
arr.push_back(make_pair(5, 7));
arr.push_back(make_pair(6, 2));
sort(arr.begin(), arr.end(),
[](pair<int, int> left, pair<int, int> right) {
int d1 = left.first * left.first + left.second * left.second;
int d2 = right.first * right.first + right.second * right.second;
return d1 < d2;
});
for (auto &p: arr) {
cout << "(" << p.first << ", " << p.second << ")" << endl;
}
return 0;
}
//如 delegate bool FuncType(ref int num); FuncType func1; func1 = num => true; //错 func1 = (ref num) => true;//错 func1 = (ref int num) => true;//ok //并且,当一个参数书写类型,其他参数也要书写,总之很烦。
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有