其他未完成
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
// 函数对象
|
||||
// 仿函数: 一元,二元仿函数
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
// 一元仿函数
|
||||
class A
|
||||
{
|
||||
public:
|
||||
A(int n) : n(n) {}
|
||||
int n;
|
||||
// 重载 operator() 运算符,只要调用 A 对象的 (int x) ,就会给对象的对应成员对象加一个 x
|
||||
A &operator()(int i)
|
||||
{
|
||||
this->n += i;
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
|
||||
void print(A a, int n) // A a = A(30)
|
||||
{
|
||||
a(n);
|
||||
cout << "a.n = " << a.n << endl;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
// 打印 a 对象的值加额外的值的结果
|
||||
print(A(20), 3); // A(20) 会创建一个匿名对象
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
// 谓词
|
||||
// gt 大于, ge 大于等于
|
||||
// lt 小于, le 小于等于
|
||||
// eq 等于, ne 小于等于
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
#include <cstdlib>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class GTFive // 一元谓词
|
||||
{
|
||||
public:
|
||||
bool operator()(int n)
|
||||
{
|
||||
return n > 5;
|
||||
}
|
||||
};
|
||||
|
||||
class GT // 二元谓词
|
||||
{
|
||||
public:
|
||||
bool operator()(const int &n1, const int &n2)
|
||||
{
|
||||
return n1 > n2;
|
||||
}
|
||||
};
|
||||
|
||||
void printGTF(GTFive gtf, int *p, int size)
|
||||
{
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
if (gtf(p[i]))
|
||||
cout << p[i] << " ";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
void print(int *p, int size, GT gt, int n)
|
||||
{
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
if (gt(p[i], n))
|
||||
cout << p[i] << " ";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int ms[] = {1, 2, 4, 7, 9, 20};
|
||||
cout << "大于 5 的元素" << endl;
|
||||
printGTF(GTFive(), ms, 6);
|
||||
|
||||
cout << "大于 7 的元素" << endl;
|
||||
print(ms, 6, GT(), 7);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
// 算术类函数对象
|
||||
// 算术类函数对象
|
||||
#include <iostream>
|
||||
#include <set>
|
||||
#include <algorithm>
|
||||
|
||||
using namespace std;
|
||||
|
||||
// template <typename T>
|
||||
void print(typename set<int>::const_iterator start,
|
||||
typename set<int>::const_iterator end, plus<int> pl, int m)
|
||||
{
|
||||
for (; start != end; start++)
|
||||
{
|
||||
cout << pl(*start, m) << " ";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
void print(typename set<int>::const_iterator start,
|
||||
typename set<int>::const_iterator end, multiplies<int> mul, int m)
|
||||
{
|
||||
for (; start != end; start++)
|
||||
{
|
||||
cout << mul(*start, m) << " ";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int m[] = {1, 2, 3, 4, 8, 5, 1, 2};
|
||||
// set 会自动过滤重复,并排序
|
||||
set<int> s(m, m + sizeof(m) / sizeof(m[0]));
|
||||
print(s.begin(), s.end(), plus<int>(), 1);
|
||||
print(s.begin(), s.end(), multiplies<int>(), 3);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
#include <iostream>
|
||||
#include <set>
|
||||
#include <algorithm>
|
||||
|
||||
using namespace std;
|
||||
|
||||
// template <typename T>
|
||||
void print(typename set<int>::const_iterator start,
|
||||
typename set<int>::const_iterator end, plus<int> pl, int m)
|
||||
{
|
||||
for (; start != end; start++)
|
||||
{
|
||||
cout << pl(*start, m) << " ";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
void print(typename set<int>::const_iterator start,
|
||||
typename set<int>::const_iterator end, multiplies<int> mul, int m)
|
||||
{
|
||||
for (; start != end; start++)
|
||||
{
|
||||
cout << mul(*start, m) << " ";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
void show(int n)
|
||||
{
|
||||
cout << n << " ";
|
||||
}
|
||||
|
||||
// 二元仿函数
|
||||
class PrintPlus : public binary_function<int, int, void>
|
||||
{
|
||||
public:
|
||||
void operator()(const int &n1, const int &n2) const
|
||||
{
|
||||
cout << n1 << "+" << n2 << " = " << n1 + n2 << endl;
|
||||
}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
int m[] = {1, 2, 3, 4, 8, 5, 1, 2};
|
||||
// set 会自动过滤重复,并排序
|
||||
set<int> s(m, m + sizeof(m) / sizeof(m[0]));
|
||||
// print(s.begin(), s.end(), plus<int>(), 1);
|
||||
// print(s.begin(), s.end(), multiplies<int>(), 3);
|
||||
|
||||
// for_each(s.begin(), s.end(), show);
|
||||
// cout << endl;
|
||||
|
||||
// PrintPlus 测试
|
||||
// for (int i = 0; i < 6; i++)
|
||||
for_each(s.begin(), s.end(), bind1st(PrintPlus(), i));
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
#include <iostream>
|
||||
#include <set>
|
||||
#include <algorithm>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class PrintGt5Adapter : public binary_function<int, int, void>
|
||||
{
|
||||
public:
|
||||
void operator()(const int &n1, const int &n2) const
|
||||
{
|
||||
if (n1 > n2)
|
||||
cout << n1 << " ";
|
||||
}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
int m[] = {1, 2, 3, 4, 8, 5, 1, 2};
|
||||
set<int> s(m, m + sizeof(m) / sizeof(m[0]));
|
||||
|
||||
for_each(s.begin(), s.end(), bind2nd(PrintGt5Adapter(), 2));
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
// 取反适配器
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
|
||||
using namespace std;
|
||||
// 自定义一个适配器,只需要容器中元素即可。
|
||||
class GT5 : public unary_function<int, bool>
|
||||
{
|
||||
public:
|
||||
bool operator()(const int &n) const
|
||||
{
|
||||
return n > 5;
|
||||
}
|
||||
};
|
||||
|
||||
class GTN : public binary_function<int, int, bool>
|
||||
{
|
||||
public:
|
||||
bool operator()(const int &n1, const int &n2) const
|
||||
{
|
||||
return n1 > n2;
|
||||
}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
int m[] = {1, 2, 2, 3, 5, 10};
|
||||
vector<int> v;
|
||||
v.assign(m, m + sizeof(m) / sizeof(m[0]));
|
||||
|
||||
// find_if(start, end, _callback) 返回查找到的第一个迭代器的位置
|
||||
vector<int>::iterator ret = find_if(v.begin(), v.end(), not1(bind2nd(greater<int>(), 3)));
|
||||
cout << *ret << endl;
|
||||
|
||||
ret = find_if(v.begin(), v.end(), GT5());
|
||||
cout << *ret << endl;
|
||||
|
||||
ret = find_if(v.begin(), v.end(), bind2nd(GTN(), 3));
|
||||
cout << *ret << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
// 函数指针适配
|
||||
// 使用 ptr_fun<>() 适配函数指针
|
||||
// <> 中的第一个参数是函数指针的类型,第二个参数是函数指针的参数类型,第三个参数是函数指针的返回值类型
|
||||
// bind1st() 和 bind2nd() 适配函数对象
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
|
||||
using namespace std;
|
||||
|
||||
bool gtn(int n1, int n2)
|
||||
{
|
||||
return n1 > n2;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int m[] = {1, 2, 2, 3, 5, 10};
|
||||
vector<int> v;
|
||||
v.assign(m, m + sizeof(m) / sizeof(m[0]));
|
||||
|
||||
vector<int>::iterator ret = find_if(v.begin(), v.end(), bind2nd(ptr_fun<int, int, bool>(gtn), 3));
|
||||
cout << *ret << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
// mem_fn 有两个重载版本:
|
||||
// 用法1:mem_fn(&MyClass::printMessage) 用于将成员函数转换为函数对象
|
||||
// 用法2:mem_fn(&MyClass::printMessage)(&obj, "hello world") 用于调用成员函数
|
||||
// mem_fn 是一个函数模板,它接受一个成员函数指针或成员函数引用,并返回一个函数对象,该对象可以调用相应的成员函数。
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Stu
|
||||
{
|
||||
private:
|
||||
string name;
|
||||
int age;
|
||||
|
||||
public:
|
||||
Stu(const string &name, int age) : name(name), age(age) {}
|
||||
|
||||
public:
|
||||
void show()
|
||||
{
|
||||
cout << "name is " << name << ", age is " << age << endl;
|
||||
}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
vector<Stu> vs;
|
||||
vs.push_back(Stu("disen", 18));
|
||||
vs.push_back(Stu("lucy", 20));
|
||||
vs.push_back(Stu("jack", 15));
|
||||
vs.push_back(Stu("mack", 19));
|
||||
|
||||
// 遍历容器中所有成员,成员函数作为仿函数时,则通过容器成员调用它的仿函数
|
||||
for_each(vs.begin(), vs.end(), mem_fun_ref(&Stu::show));
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <functional>
|
||||
|
||||
using namespace std;
|
||||
using namespace std::placeholders;
|
||||
|
||||
int main()
|
||||
{
|
||||
int m[] = {1, 2, 2, 3, 5, 10};
|
||||
vector<int> v;
|
||||
v.assign(m, m + sizeof(m) / sizeof(m[0]));
|
||||
|
||||
int target = 3;
|
||||
|
||||
// 使用 std::bind 适配 std::equal_to
|
||||
vector<int>::iterator ret = adjacent_find(v.begin(), v.end(), bind(equal_to<int>(), _1, 3));
|
||||
cout << *(++ret) << endl;
|
||||
|
||||
if (ret != v.end())
|
||||
{
|
||||
cout << "Found adjacent elements equal to " << target << "." << endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
cout << "No adjacent elements equal to " << target << " found." << endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user