使用时的特点:
- // 1.函数对象在使用时,可以像普通函数那样调用,可以有参数,可以有返回值
- class MyAdd
- {
- public:
- int operator()(int v1, int v2)
- {
- return v1 + v2;
- }
- };
- void test01()
- {
- MyAdd myadd;
- cout << myadd(10, 20) << endl;
- }
-
- // 2.函数对象超出普通函数的概念,函数对象可以有自己的状态
- class MyPrint
- {
- public:
- MyPrint()
- {
- this->count = 0;
- }
- void operator()(string text)
- {
- cout << text << endl;
- this->count++;
- }
- int count; // 记录调用次数
- };
-
- void test02()
- {
- MyPrint mp;
- mp("Hello");
- cout << mp.count << endl;
- }
-
- // 3.函数对象可以作为参数传递
- void doPrint(MyPrint &mp, string text)
- {
- mp(text);
- }
- void test03()
- {
- MyPrint mp;
- doPrint(mp, "hello");
- }
- // 一元谓词
- class GreaterFive
- {
- public:
- bool operator()(int num)
- {
- return num > 5;
- }
- };
-
- void test01()
- {
- vector<int> v;
- for(int i=0; i<10; i++)
- {
- v.push_back(i);
- }
- // 查找容器中是否有大于5的数字
- // GreaterFive()是一个匿名的函数对象
- // 返回值是一个迭代器
- vector<int>::iterator it = find_if(v.begin(), v.end(), GreaterFive());
- if(it == v.end())
- {
- cout << "未找到" << endl;
- }
- else
- {
- cout << "找到" << *it << endl;
- }
- }
- // 二元谓词
- class MyCompare
- {
- public:
- bool operator()(int num1, int num2)
- {
- return num1 > num2;
- }
- };
-
- void test01()
- {
- vector<int> v;
- v.push_back(10);
- v.push_back(30);
- v.push_back(20);
- v.push_back(60);
- v.push_back(50);
-
- sort(v.begin(), v.end());
- for(vector<int>::iterator it = v.begin(); it != v.end(); it++)
- {
- cout << *it << " ";
- }
- cout << endl;
-
- // 使用仿函数,可以改变排序规则为降序
- sort(v.begin(), v.end(), MyCompare());
- for(vector<int>::iterator it = v.begin(); it != v.end(); it++)
- {
- cout << *it << " ";
- }
- cout << endl;
- }
分类:
用法:
- #include
-
- // negate 一元仿函数 取反仿函数
- void test01()
- {
- negate<int> n;
- cout << n(50) << endl; // -50
- }
-
- // plus 二元仿函数 加法
- void test02()
- {
- plus<int> p;
- cout << p(10, 20) << endl;
- }
- #include
-
- class MyCompare
- {
- public:
- bool operator()(int num1, int num2)
- {
- return num1 > num2;
- }
- };
-
- void test01()
- {
- vector<int> v;
- v.push_back(10);
- v.push_back(30);
- v.push_back(40);
- v.push_back(20);
- v.push_back(50);
-
- for(vector<int>::iterator it=v.begin(); it != v.end(); it++)
- {
- cout << *it << " ";
- }
- cout << endl;
-
- // 降序
- // 等价于sort(v.begin(), v.end(), MyCompare());
- // greater
()是内建函数对象(仿函数) - sort(v.begin(), v.end(), greater<int>());
- for(vector<int>::iterator it=v.begin(); it != v.end(); it++)
- {
- cout << *it << " ";
- }
- cout << endl;
- }
- void test01()
- {
- vector<bool> v;
- v.push_back(true);
- v.push_back(false);
- v.push_back(true);
- v.push_back(false);
-
- for(vector<bool>::iterator it=v.begin(); it!=v.end(); it++)
- {
- cout << *it << " ";
- }
- cout << endl; // 1 0 1 0
-
- // 利用逻辑非 将容器v搬运到容器v2中,并执行取反操作
- vector<bool> v2;
- v2.resize(v.size()); // 必须提前开辟空间,指定大小
-
- transform(v.begin(), v.end(), v2.begin(), logical_not<bool>());
-
- for(vector<bool>::iterator it=v2.begin(); it!=v2.end(); it++)
- {
- cout << *it << " ";
- }
- cout << endl; // 0 1 0 1
- }
for_each:遍历容器
- // 普通函数
- void print01(int val)
- {
- cout << val << endl;
- }
-
- // 仿函数
- class print02
- {
- public:
- void operator()(int val)
- {
- cout << val << endl;
- }
- };
-
- void test01()
- {
- vector<int> v;
- for(int i=0; i<10; i++)
- {
- v.push_back(i);
- }
-
- for_each(v.begin(), v.end(), print01);
- cout << endl;
-
- for_each(v.begin(), v.end(), print02());
- cout << endl;
- }
transform:搬运容器到另一个容器中
- class Transform
- {
- public:
- int operator()(int val)
- {
- return val;
- }
- };
-
- class Myprint
- {
- public:
- void operator()(int val)
- {
- cout << val << " ";
- }
- };
-
- void test01()
- {
- vector<int> v;
- for(int i=0; i<10; i++)
- {
- v.push_back(i);
- }
-
- vector<int> v2; // 目标容器
- v2.resize(v.size()); // 目标容器需要提前开辟空间
- transform(v.begin(), v.end(), v2.begin(), Transform());
- for_each(v2.begin(), v2.end(), Myprint());
- cout << endl;
- }
find:查找元素**(返回值是一个迭代器)**
- // 查内置数据类型
- void test01()
- {
- vector<int> v;
- for(int i=0; i<10; i++)
- {
- v.push_back(i);
- }
- vector<int>::iterator it = find(v.begin(), v.end(), 5);
- if(it == v.end())
- {
- cout << "找不到" << endl;
- }
- else
- {
- cout << "找到" << *it << endl;
- }
- }
- class Person
- {
- public:
- Person(string name, int age)
- {
- this->m_name = name;
- this->m_age = age;
- }
- // 重载== 让底层find知道如何对比person数据类型
- bool operator==(const Person &p)
- {
- if(p.m_name == this->m_name || p.m_age == this->m_age)
- {
- return true;
- }
- return false;
- }
- string m_name;
- int m_age;
- };
-
- // 查自定义数据类型
- void test02()
- {
- vector
v; - Person p1("Alice", 18);
- Person p2("Bob", 16);
- Person p3("Cycal", 20);
- Person p4("Davil", 19);
- v.push_back(p1);
- v.push_back(p2);
- v.push_back(p3);
- v.push_back(p4);
-
- Person pp("Bob", 16);
- vector
::iterator it = find(v.begin(), v.end(), pp); - if(it == v.end())
- {
- cout << "找不到" << endl;
- }
- else
- {
- cout << "找到" << it->m_name << " " << it->m_age << endl;
- }
- }
find_if:按条件查找元素
- class GreaterFive
- {
- public:
- bool operator()(int val)
- {
- return val > 5;
- }
- };
-
- // 查内置数据类型
- void test01()
- {
- vector<int> v;
- for(int i=0; i<10; i++)
- {
- v.push_back(i);
- }
- vector<int>::iterator it = find_if(v.begin(), v.end(), GreaterFive());
- if(it == v.end())
- {
- cout << "找不到" << endl;
- }
- else
- {
- cout << "找到" << *it << endl;
- }
- }
- class Person
- {
- public:
- Person(string name, int age)
- {
- this->m_name = name;
- this->m_age = age;
- }
- // 重载== 让底层find知道如何对比person数据类型
- bool operator==(const Person &p)
- {
- if(p.m_name == this->m_name || p.m_age == this->m_age)
- {
- return true;
- }
- return false;
- }
- string m_name;
- int m_age;
- };
-
- class Greater20
- {
- public:
- bool operator()(Person p)
- {
- return p.m_age > 20;
- }
- };
-
- // 查自定义数据类型
- void test02()
- {
- vector
v; - Person p1("Alice", 18);
- Person p2("Bob", 16);
- Person p3("Cycal", 22);
- Person p4("Davil", 19);
- v.push_back(p1);
- v.push_back(p2);
- v.push_back(p3);
- v.push_back(p4);
-
- vector
::iterator it = find_if(v.begin(), v.end(), Greater20()); - if(it == v.end())
- {
- cout << "找不到" << endl;
- }
- else
- {
- cout << "找到" << it->m_name << " " << it->m_age << endl;
- }
- }
adjacent_find:查找相邻重复元素
- void test01()
- {
- vector<int> v;
- v.push_back(0);
- v.push_back(2);
- v.push_back(1);
- v.push_back(1);
- v.push_back(3);
- v.push_back(0);
- v.push_back(2);
- v.push_back(1);
-
- vector<int>::iterator it = adjacent_find(v.begin(), v.end());
- if(it == v.end())
- {
- cout << "找不到相邻重复元素" << endl;
- }
- else
- {
- cout << "找到相邻重复元素" << *it << endl;
- }
- }
binary_search:二分查找法(查找指定元素,查到返回true,查不到返回false)(在无序序列中不能使用)
- void test01()
- {
- vector<int> v;
- for(int i=0; i<10; i++)
- {
- v.push_back(i);
- }
- bool tag = binary_search(v.begin(), v.end(), 9);
- if(tag == true)
- {
- cout << "找到了" << endl;
- }
- else
- {
- cout << "找不到" << endl;
- }
- }
count:统计元素个数
- // 统计内置数据类型
- void test01()
- {
- vector<int> v;
- for(int i=0; i<10; i++)
- {
- v.push_back(i);
- }
- int num = count(v.begin(), v.end(), 2);
- cout << num << endl;
- }
- class Person
- {
- public:
- Person(string name, int age)
- {
- this->m_name = name;
- this->m_age = age;
- }
-
- // 重载== 让底层find知道如何对比person数据类型
- bool operator==(const Person &p)
- {
- if(p.m_age == this->m_age)
- {
- return true;
- }
- return false;
- }
- string m_name;
- int m_age;
- };
-
- // 统计自定义数据类型
- void test02()
- {
- vector
v; - Person p1("Alice", 18);
- Person p2("Bob", 16);
- Person p3("Cycal", 22);
- Person p4("Davil", 16);
- v.push_back(p1);
- v.push_back(p2);
- v.push_back(p3);
- v.push_back(p4);
-
- Person pp("Tim", 16);
- int num = count(v.begin(), v.end(), pp);
- cout << num << endl;
- }
count_if:按条件统计元素个数
- class GreaterFive
- {
- public:
- bool operator()(int val)
- {
- return val > 5;
- }
- };
-
- // 查内置数据类型
- void test01()
- {
- vector<int> v;
- for(int i=0; i<10; i++)
- {
- v.push_back(i);
- }
- int count = count_if(v.begin(), v.end(), GreaterFive());
- cout << count << endl;
- }
- class Person
- {
- public:
- Person(string name, int age)
- {
- this->m_name = name;
- this->m_age = age;
- }
- string m_name;
- int m_age;
- };
-
- class MyCompare
- {
- public:
- bool operator()(const Person &p)
- {
- return p.m_age > 18;
- }
- };
-
- // 查自定义数据类型
- void test02()
- {
- vector
v; - Person p1("Alice", 18);
- Person p2("Bob", 16);
- Person p3("Cycal", 20);
- Person p4("Davil", 19);
- v.push_back(p1);
- v.push_back(p2);
- v.push_back(p3);
- v.push_back(p4);
-
- int count = count_if(v.begin(), v.end(), MyCompare());
- cout << count << endl;
- }