目录
1. 函数对象
class myPrint {
public:
void operator()(int num) {
cout << "num " << num << endl;
}
};
void test() {
myPrint print;//myPrint是一个类而不是函数
print(111);
myPrint()(1000);//使用匿名对象调用仿函数
}
class myPrint {
public:
void operator()(int num) {
cout << "num " << num << endl;
count++;//计算使用了多少次该方法
}
int count = 0;
};
void test() {
//函数对象超出普通函数概念,内部可以保存状态
myPrint print;
print(111);
print(111);
print(111);
print(111);
cout << "myPrint的使用次数: " << print.count << endl;
}
class myPrint {
public:
void operator()(int num) {
cout << "num " << num << endl;
count++;//计算使用了多少次该方法
}
int count = 0;
};
//函数对象作为参数
void doPrint(myPrint print, int num) {
print(num);
}
void test() {
doPrint(myPrint(),20);
}
2. 谓词使用
//仿函数
class greaterThan {
public:
bool operator()(int val) {
return val > 20;
}
};
//一元谓词
void test() {
vector<int> v;
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(40);
v.push_back(50);
//查找第一个大于20的数字
//第三个参数应该为函数对象,可以先声明对象,也可以使用匿名对象
vector<int>::iterator pos = find_if(v.begin(), v.end(), greaterThan());
if (pos != v.end()) {
cout << "找到大于20的数字为: " << *pos << endl;
}
else
cout << "未找到" << endl;
}
//仿函数
class greaterThan {
public:
bool operator()(int v1,int v2)const {
return v1 > v2;
}
};
//二元谓词
void test() {
vector<int> v;
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(40);
v.push_back(50);
//查找第一个大于20的数字
//第三个参数应该为函数对象,可以先声明对象,也可以使用匿名对象
sort(v.begin(), v.end(), greaterThan());
//匿名函数 lambda表达式 [](){}
for_each(v.begin(), v.end(), [](int val) {cout << val << " "; });
}
3. 内建函数对象
void test() {
//template<class T> T negate<T> 取反仿函数
negate<int> n;
cout << n(10) << endl;
//template<class T>T plus<T> 加法
plus<int> p;
cout << p(2, 5) << endl;
//template<class T>bool greater<T> 大于
vector<int>v;
v.push_back(10);
v.push_back(30);
v.push_back(50);
v.push_back(20);
v.push_back(40);
sort(v.begin(), v.end(), greater<int>());
//匿名函数 lambda表达式 [](){}
for_each(v.begin(), v.end(), [](int val) {cout << val << " "; });
}
4. 适配器使用
class myPrint:public binary_function<int,int,void>
{
public:
void operator()(int v, int start) const{
cout << "v = " << v << " start = " << start << " v + start = " << v + start << " " << endl;
}
};
//适配器,将两个参数变为一个参数
//第一步使用bind绑定数据
//继承类 binary_function<参数类型1,参数类型2,返回值类型>
//加const修饰 operator()
void test() {
vector<int>v;
for (int i = 0; i < 10; ++i) {
v.push_back(i);
}
cout << "请输入起始值: " << endl;
int num;
cin >> num;
for_each(v.begin(),v.end(),bind2nd(myPrint(),num));
}
class greaterThan {
public:
bool operator()(int v) {
return v > 5;
}
};
//取反适配器
void test() {
//一元取反
vector<int>v;
for (int i = 0; i < 10; ++i) {
v.push_back(i);
}
//查找大于5的数字
vector<int>::iterator pos = find_if(v.begin(), v.end(), greaterThan());
if (pos != v.end()) {
cout << "找到大于5的数字为: " << *pos << endl;
}
else
cout << "未找到" << endl;
}
class greaterThan:public unary_function<int,bool>
{
public:
bool operator()(int v) const{
return v > 5;
}
};
//取反适配器
void test() {
//一元取反
vector<int>v;
for (int i = 0; i < 10; ++i) {
v.push_back(i);
}
//查找大于5的数字
//当需要改为查找小于5的数字时
vector<int>::iterator pos = find_if(v.begin(), v.end(), not1(greaterThan()));
//vector<int>::iterator pos = find_if(v.begin(), v.end(), not1(bind2nd(greater<int>(),5)));
if (pos != v.end()) {
cout << "找到小于5的数字为: " << *pos << endl;
}
else
cout << "未找到" << endl;
}
void myPrint(int v) {
cout << v << " ";
}
void myPrint2(int v, int start) {
cout << v + start << " ";
}
//函数指针适配器
void test() {
vector<int>v;
for (int i = 0; i < 10; ++i) {
v.push_back(i);
}
//将函数指针适配为(改为)函数对象
//ptr_fun
for_each(v.begin(), v.end(), myPrint);
cout << endl;
for_each(v.begin(), v.end(), bind2nd(ptr_fun(myPrint2),100));
}
class Person {
public:
string name;
int age;
Person(string name, int age) {
this->name = name;
this->age = age;
}
void showPerson() {
cout << "成员函数中姓名: " << name << "年龄: " << age << endl;
}
};
void printPerson(Person& p) {
cout << "姓名: " << p.name << "年龄: " << p.age << endl;
}
//成员函数适配器
void test() {
vector<Person>v;
Person p1("aaa", 10);
Person p2("bbb", 20);
Person p3("ccc", 30);
Person p4("ddd", 40);
Person p5("eee", 50);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);
for_each(v.begin(), v.end(), printPerson);
cout << "-----------------------------" << endl;
//成员函数适配器
//mem_fun_ref
for_each(v.begin(), v.end(), mem_fun_ref(&Person::showPerson));
}
5. 常用算法
5.1 遍历算法
struct myPrint {
int num;
void operator()(int v) {
cout << v << " ";
num++;
}
};
//for_each可以保存内部记录,有返回值
void test() {
vector<int>v;
for (int i = 0; i < 10; ++i) {
v.push_back(i);
}
myPrint print = for_each(v.begin(), v.end(), myPrint());
cout << print.num << endl;
}
struct myPrint:public binary_function<int,int,void> {
void operator()(int v, int start) const{
cout << v + start << " ";
}
};
//for_each可以绑定参数进行输出
void test() {
vector<int>v;
for (int i = 0; i < 10; ++i) {
v.push_back(i);
}
for_each(v.begin(), v.end(), bind2nd(myPrint(),1000));
}
class transForm {
public:
int operator()(int val) {
return val + 10;
}
};
//transform算法 将指定容器区间元素搬运到另一容器中
void test() {
vector<int>v;
for (int i = 0; i < 10; ++i) {
v.push_back(i);
}
vector<int>vTarget;//目标容器
//需要先为目标容器开辟内存
vTarget.resize(v.size());
transform(v.begin(), v.end(), vTarget.begin(), transForm());
for_each(vTarget.begin(), vTarget.end(), [](int val) {cout << val << " "; });
}
class transForm {
public:
int operator()(int val1, int val2) {
return val1 + val2;
}
};
//transform算法 将指定容器区间元素搬运到另一容器中
//第二种用法 将两个容器中的数据相加后搬运到目标容器
void test() {
vector<int>v1;
vector<int>v2;
for (int i = 0; i < 10; ++i) {
v1.push_back(10 + i);
v2.push_back(20 + i);
}
vector<int>vTarget;//目标容器
//需要先为目标容器开辟内存
vTarget.resize(v1.size());
transform(v1.begin(), v1.end(), v2.begin(), vTarget.begin(), transForm());
for_each(vTarget.begin(), vTarget.end(), [](int val) {cout << val << " "; });
}
5.2 常用查找算法
void test() {
vector<int>v1;
for (int i = 0; i < 10; ++i) {
v1.push_back(i);
}
vector<int>::iterator pos = find(v1.begin(), v1.end(), 5);
if (pos != v1.end()) {
cout << "找到了数据: " << *pos << endl;
}
else
cout << "未找到" << endl;
}
class Person {
public:
string name;
int age;
Person(string name, int age) {
this->name = name;
this->age = age;
}
//重载关系运算符
bool operator==(const Person& p) {
if (this->name == p.name && this->age == p.age) {
return true;
}
return false;
}
};
void test() {
//find查找自定义数据类型
vector<Person>v;
Person p1("aaa", 10);
Person p2("bbb", 20);
Person p3("ccc", 30);
Person p4("ddd", 40);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
vector<Person>::iterator pos = find(v.begin(), v.end(), p2);
if (pos != v.end()) {
cout << "找到了数据: " << (*pos).name << endl;
}
else
cout << "未找到" << endl;
}
void test() {
vector<int>v;
v.push_back(2);
v.push_back(3);
v.push_back(4);
v.push_back(5);
v.push_back(5);
v.push_back(6);
v.push_back(2);
vector<int>::iterator pos = adjacent_find(v.begin(), v.end());
if (pos != v.end()) {
cout << "找到了相邻数据: " << *pos << endl;
}
else
cout << "未找到" << endl;
}
void test() {
vector<int>v;
for (int i = 0; i < 10; ++i) {
v.push_back(i);
}
bool ret = binary_search(v.begin(), v.end(), 4);
if (ret)
cout << "找到了4" << endl;
else
cout << "没有找到" << endl;
}
class greaterThan {
public:
bool operator()(int v) {
return v >= 4;
}
};
void test() {
vector<int>v;
for (int i = 0; i < 10; ++i) {
v.push_back(i);
}
v.push_back(4);
v.push_back(4);
v.push_back(4);
v.push_back(4);
int num = count(v.begin(), v.end(), 4);
cout << "4的个数为: " << num << endl;
num = count_if(v.begin(), v.end(), greaterThan());
cout << "大于等于4的个数为: " << num << endl;
}
5.3 常用排序算法
void test() {
vector<int>v;
for (int i = 0; i < 10; ++i) {
v.push_back(i);
}
random_shuffle(v.begin(), v.end());
for_each(v.begin(), v.end(), [](int val) {cout << val << " "; });
}
int main(){
srand((unsigned int)time(NULL));
test();
return 0;
}
5.4 常用拷贝和替换算法
5.4 常用算数生成算法
6. 案例—演讲比赛
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <stdexcept>
#include <deque>
#include <ctime>
#include <list>
#include <set>
#include <map>
#include <functional>
#include <numeric>
using namespace std;
class Speaker {
public:
string name;
int score[3];//得分数组,记录多轮成绩
};
void createSpeaker(vector<int>& v, map<int, Speaker>& m) {
string nameSeed = "ABCDEFGHIJKLMNOPQRSTUVWX";
for (int i = 0; i < nameSeed.size(); ++i) {
string name = "选手";
name += nameSeed[i];
Speaker sp;
sp.name = name;
for (int j = 0; j < 3; ++j) {
sp.score[j] = 0;
}
v.push_back(i + 100);//选手编号为100-123
m.insert(make_pair(i + 100, sp));
}
}
void draw(vector<int>v) {
//洗牌,打乱容器中数据的顺序
random_shuffle(v.begin(), v.end());
}
void contest(int index, vector<int>& v1, map<int, Speaker>& m, vector<int>& v2) {
//index代表第几轮得分,v1是比赛选手编号,m是选手编号和具体选手,v2是晋级选手编号
multimap<int, int, greater<int>> groupMap;//临时容器,从大到小排序,key为分数,value编号
int num = 0;//六个人选三人出来
for (vector<int>::iterator it = v1.begin(); it != v1.end(); ++it) {
num++;
deque<int>d;//打分
for (int i = 0; i < 10; ++i) {
int score = rand() % 41 + 60;
d.push_back(score);
}
//排序
sort(d.begin(), d.end());
//去除最高最低分
d.pop_back();
d.pop_front();
//累计分数
int sum = accumulate(d.begin(), d.end(), 0);//从0开始计算
int avg = sum / d.size();
//将平均分放进到m容器中
m[*it].score[index - 1] = avg;
//每六个人取前三名晋级
//临时容器保存6个人
//临时容器存入数据
groupMap.insert(make_pair(avg, *it));
if (num % 6 == 0) {
//cout << "小组比赛成绩: " << endl;
//for (multimap<int, int, greater<int>>::iterator mit = groupMap.begin(); mit != groupMap.end(); mit++){
// cout << "编号: " << mit->second << " 姓名: " << m[mit->second].name << " 得分" << m[mit->second].score[index - 1] << endl;
//}
//取前三名
int count = 0;
for (multimap<int, int, greater<int>>::iterator mit = groupMap.begin(); mit != groupMap.end(), count<3; mit++,count++) {
//晋级容器 获取数据
v2.push_back(mit->second);
}
groupMap.clear();//清空临时容器
}
}
}
void showScore(int index, vector<int>& v, map<int, Speaker>& m) {
cout << "第" << index << "轮 比赛成绩如下: " << endl;
for (map<int, Speaker>::iterator it = m.begin(); it != m.end(); it++) {
cout << "选手编号: " << it->first << "姓名: " << it->second.name << "分数: " << it->second.score[index - 1] << endl;
}
cout << "晋级选手编号: " << endl;
for (vector<int>::iterator it = v.begin(); it != v.end(); ++it) {
cout << *it << endl;
}
}
void test() {
vector<int>v1;//选手编号
vector<int>v2;//进入下一轮比赛的人员编号
map<int, Speaker>m;//存放选手编号和对应的具体选手
//创建选手
createSpeaker(v1, m);
//for (map<int, Speaker>::iterator it = m.begin(); it != m.end(); ++it) {
// cout << "编号: " << it->first << "姓名: " << it->second.name << endl;
//}
//抽签
draw(v1);
//比赛
contest(1, v1, m, v2);
//显示比赛结果
showScore(1, v2, m);
//第二轮比赛
draw(v2);
vector<int>v3;//第二轮晋级
contest(2, v2, m, v3);
showScore(2, v3, m);
//第三轮比赛
draw(v3);
vector<int> v4;
contest(3, v3, m, v4);
showScore(2, v4, m);
}
int main(){
srand((unsigned int)time(NULL));
test();
return 0;
}