1,运行效果
2, 代码示例
#include
#include
#include
#include
int add(const int a, const int b) {
return a+b;
}
int sub(const int a, const int b) {
return a-b;
}
class Add {
public:
const int operator()(const int a, const int b) {
return a+b;
}
std::vector<int> operator()(const std::vector<int> &a, const std::vector<int> &b) {
assert(a.size() == b.size());
std::vector<int> c(a.size());
std::transform(a.begin(), a.end(), b.begin(), c.begin(), add);
return c;
}
};
class Sub {
public:
const int operator()(const int a, const int b) {
return a-b;
}
std::vector<int> operator()(const std::vector<int> &a, const std::vector<int> &b) {
assert(a.size() == b.size());
std::vector<int> c(a.size());
std::transform(a.begin(), a.end(), b.begin(), c.begin(), sub);
return c;
}
};
void func(int a, int b, int (*op)(int, int)) {
std::cout << "1:" << op(a, b) << std::endl;
}
void func(int a, int b, Add &op) {
std::cout << "2:" << op(a, b) << std::endl;
}
template <typename T>
void func(int a, int b, T &op) {
std::cout << "3:" << op(a, b) << std::endl;
}
int print_elements(int val) {
std::cout << val << " ";
}
class PrintElements {
public:
void operator()(int val) {
std::cout << val << " ";
}
};
int main(){
std::cout << add(1, 2) << std::endl;
func(1, 2, add);
func(1, 2, sub);
std::cout << "----------------------------" << std::endl;
Add x;
std::cout << x(7, 2) << std::endl;
func(2, 3, x);
Sub s;
func(2, 3, s);
std::cout << "----------------------------" << std::endl;
std::vector<int> a = {1,2,3,4,5};
std::vector<int> b = {6,7,8,9,10};
std::for_each(a.begin(), a.end(), PrintElements());
std::cout << std::endl;
PrintElements print;
std::for_each(b.begin(), b.end(), print);
std::cout << std::endl;
std::vector<int> &&c = x(a, b);
std::for_each(c.begin(), c.end(), print_elements);
std::cout << std::endl;
std::vector<int> &&d = s(a, b);
std::for_each(d.begin(), d.end(), [&](int val) {std::cout << val << " ";});
std::cout << std::endl;
return 0;
}
3,lambda表达式
C++11 之 lambda函数的详细使用
【面试实战】C++11 lambda表达式的捕捉方式有哪几种
3.1,语法
3.2,捕获方式
4,c++标准库function object
4.1,六个算数运算符
#include
#include
void func_1() {
std::plus<int> m_plus;
std::cout << "plus(3,2):"<< m_plus(3,2) << std::endl;
std::minus<int> m_minus;
std::cout << "minus(3,2):" << m_minus(3,2) << std::endl;
std::negate<int> m_negate;
std::cout << "negate(3):" << m_negate(3) << std::endl;
std::multiplies<int> m_multiplies;
std::cout << "multiplies(3,2):" << m_multiplies(3,2) << std::endl;
std::divides<int> m_divides;
std::cout << "divides(3,2):" << m_divides(3,2) << std::endl;
std::modulus<int> m_modulus;
std::cout << "modulus(3,2):" << m_modulus(3,2) << std::endl;
}
template<typename T>
void func_2(T &op) {
std::cout << op(3,2) << std::endl;
}
int main(){
func_1();
std::cout << "----------------" << std::endl;
std::plus<int> m_plus;
func_2(m_plus);
std::divides<int> m_divides;
func_2(m_divides);
// function object adapter
std::cout << "----------------" << std::endl;
std::cout << std::bind2nd(m_divides, 3)(5) << std::endl;
std::cout << std::bind1st(m_divides, 3)(5) << std::endl;
return 0;
}
4.2,六个关系运算符
#include
#include
void func_1() {
std::less<int> m_less;
std::cout << "less(3,2):"<< m_less(3,2) << std::endl;
std::less_equal<int> m_less_equal;
std::cout << "less_equal(3,3):" << m_less_equal(3,3) << std::endl;
std::greater<int> m_greater;
std::cout << "greater(3,1):" << m_greater(3,1) << std::endl;
std::greater_equal<int> m_greater_equal;
std::cout << "greater_equal(2,2):" << m_greater_equal(2,2) << std::endl;
std::equal_to<int> m_equal_to;
std::cout << "equal_to(3,2):" << m_equal_to(3,2) << std::endl;
std::not_equal_to<int> m_not_equal_to;
std::cout << "not_equal_to(3,2):" << m_not_equal_to(3,2) << std::endl;
}
template<typename T>
void func_2(T op) {
std::cout << op(4) << std::endl;
}
int main(){
func_1();
// function object adapter
std::cout << "----------------" << std::endl;
std::less<int> m_less;
func_2(std::bind2nd(m_less, 2));
std::greater_equal<int> m_greater_equal;
func_2(std::bind1st(m_greater_equal, 2));
return 0;
}
4.3,三个逻辑运算符
#include
#include
void func_1() {
std::logical_and<int> m_and;
std::cout << "logical_and(3,4):"<< m_and(3,2) << std::endl;
std::logical_or<int> m_or;
std::cout << "logical_or(3,4):" << m_or(3,4) << std::endl;
std::logical_not<int> m_not;
std::cout << "logical_not(3):" << m_not(3) << std::endl;
}
template<typename T>
void func_2(T op) {
std::cout << op(0) << std::endl;
}
int main(){
func_1();
std::cout << "----------------" << std::endl;
std::logical_and<int> m_and;
func_2(std::bind2nd(m_and, 2));
std::logical_or<int> m_or;
func_2(std::bind1st(m_or, 2));
return 0;
}