#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<map>
#include<ctime>
#include<algorithm>
#include<functional>
/*案例-员工分组
公司今天招聘了十个员工(ABCDEFGHIJK) ,10名员工进入公司之后,需要指派员工在那个部门工作
员工的信息有: 姓名 工资组成; 部门划分: 美术、策划、研发
随机给10名员工分配部门和工资
通过multimap 进行信息的插入 key(部门编号) value(员工)
*/
#define CEHUA 0
#define MEISHU 1
#define YANFA 2
class Worker {
public:
string m_Name;
int m_Salary;
};
//创建员工
void createWorker(vector<Worker>& v)
{
//创建10名员工放到vector中
string nameSeed = "ABCDEFGHIJ";
for (int i = 0; i < 10; i++)
{
Worker worker;
worker.m_Name = "员工";
worker.m_Name += nameSeed[i];
worker.m_Salary = rand() % 10001 + 10000; //10000//`20000
v.push_back(worker);
}
}
//分组
void setGroup(vector<Worker>& v, multimap<int, Worker>& m)
{
for (vector<Worker>::iterator it = v.begin(); it != v.end(); it++)
{
//产生随机部门编号
int deptId = rand() % 3; //0 1 2
//将员工插入到分组中
//key部门编号,value具体员工
m.insert(make_pair(deptId,*it));
}
}
//分组显示员工
void showWorkerByGroup(multimap<int,Worker> &m)
{
// 0 A B C 1 D E 2 F G
cout << "策划部门" << endl;
multimap<int, Worker>::iterator pos = m.find(CEHUA);//找到返回下标
int count = m.count(CEHUA); //统计具体人数
int index = 0;
for (; pos != m.end() && index < count; pos++, index++)
{
cout << "姓名" << pos->second.m_Name<< "工资" << pos->second.m_Salary<< endl;
}
cout << "----------------------------------------------" << endl;
cout << "美术部门:"<< endl;
pos = m.find(MEISHU);
count = m.count(MEISHU); //统计具体人数
index = 0;
for (; pos != m.end() && index < count; pos++, index++)
{
cout << "姓名" << pos->second.m_Name << "工资" << pos->second.m_Salary << endl;
}
cout << "----------------------------------------------" << endl;
cout << "研发部门:" << endl;
pos = m.find(YANFA);
count = m.count(YANFA); //统计具体人数
index = 0;
for (; pos != m.end() && index < count; pos++, index++)
{
cout << "姓名" << pos->second.m_Name << "工资" << pos->second.m_Salary << endl;
}
}
//分组后,将员工部门编号为key,具体工作为value,放到multimap容器中
//分部门显示员工信息
void test_example_01()
{
srand((unsigned int)time(NULL)); //利用系统时间 - 真实随机性
//将员工放入到容器中
vector<Worker>vWorker;
createWorker(vWorker);
//测试
/*
for (vector<Worker>::iterator it = vWorker.begin(); it != vWorker.end(); it++)
{
cout<< "姓名:" << it->m_Name << " 工资:" << it->m_Salary << endl;
}
*/
//员工分组
multimap<int, Worker>mWorker;
setGroup(vWorker,mWorker);
//分组显示员工
showWorkerByGroup(mWorker);
}
/*STL - 函数对象
概念:
重载函数调用操作符的类,其对象常称为函数对象
函数对象使用重载的()时,行为类似函数调用,也叫仿函数
本质:
函数对象(仿函数)是一个类,不是一个函数
函数对象使用
特点:
*函数对象在使用时,可以像普通函数那样调用,可以有参数,可以有返回值
*函数对象超出普通函数的概念,函数对象可以有自己的状态
*函数对象可以作为参数传递
*/
class MyAdd {
public:
int operator()(int v1, int v2)
{
return v1 + v2;
}
};
//1.函数对象在使用时,可以像普通函数那样调用,可以有参数,可以有返回值
void test_vmfunc_01()
{
MyAdd myAdd;
cout << myAdd(5,15) << endl;
}
//2.函数对象超出普通函数的概念,函数对象可以有自己的状态
class MyPrint {
public:
MyPrint()
{
this->count = 0;//初始化
}
void operator()(string test)
{
cout << test << endl;
this->count++;//统计使用次数
}
int count;//内部自己状态
};
void test_vmfunc_02()
{
MyPrint myPrint;
myPrint("Hello world");
myPrint("Hello world");
myPrint("Hello world");
myPrint("Hello world");
cout << "myPrint调用次数为:" << myPrint.count <<endl;
}
//3.函数对象可以作为参数传递
void doPrint(MyPrint &mp,string test) //传对象,打印值
{
mp(test);
}
void test_vmfunc_03()
{
MyPrint myPrint;
doPrint(myPrint, "Hello c++");
}
/*谓词
概念:
返回值bool类型的仿函数称为谓词
如果operator()接受一个参数,那么叫做一元谓词
如果operator()接受二个参数,那么叫做二元谓词
*/
//一元谓词
class GreaterFive {
public:
bool operator()(int val) //返回bool型的仿函数 称为谓词 ,传一个参数,一元谓词
{
return val > 5;//找到大于5的值
}
};
void test___01()
{
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()); //find_if库实际for循环 返回迭代器
if (it == v.end())
{
cout <<"未找到" << endl;
}
else
{
cout << "找到了大于5的数字为: " << *it <<endl;
}
}
//二元谓词
class Mycompare_02 {
public:
bool operator()(int val1,int val2)
{
return val1 > val2;
}
};
void test___02()
{
vector<int>v;
v.push_back(10);
v.push_back(40);
v.push_back(20);
v.push_back(30);
v.push_back(50);
sort(v.begin(),v.end());
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout<< *it <<" ";
}
cout << endl;
cout << "----------------------" << endl;
sort(v.begin(), v.end(), Mycompare_02());
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
/*STL内建函数对象
分类:
算术仿函数 + - * /
关系仿函数 > < =
逻辑仿函数 & | ^
用法:
这些仿函数所产生的对象,用法和一般函数完全相同
***使用内建函数对象,需要引用头文件 #include<functional>
*/
/*算术仿函数
*实现四则运算
*其中negate是一元运算,其他都是二元运算
template<class T> T plus<T> //加法仿函数
template<class T> T minus<T> //减法仿函数
template<class T> T multiplies<T> //乘法仿函数
template<class T> T negate<T> //除法仿函数
template<class T> T modulus<T> //取模仿函数
template<class T> T negate<T> //取反仿函数
*/
void test_func__01()
{
negate<int>n;
//取反
cout << n(50) << endl;
}
//plus 二元仿函数 加法
void test_func__02()
{
plus<int>p;
//加法
cout << p(10,20) << endl;
}
/*算术仿函数
*实现四则运算
*其中negate是一元运算,其他都是二元运算
template<class T> bool equal_to<T> //等于
template<class T> bool not_equal<T> //不等于
template<class T> bool graeter<T> //大于
template<class T> bool graeter_equal<T> //大等于
template<class T> bool less<T> //小于
template<class T> bool less_equal<T> //小等于
*/
//大于 graeter
void test_func__03()
{
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_02()); //Mycompare_02() 小括号别漏
//greater<int>()本身意义就是大于号 " > " 内建函数对象 ,以后直接用greater<int>() 降序
sort(v.begin(), v.end(),greater<int>());
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
/*逻辑仿函数 实际使用较少
template<class T> bool logic_and<T> //逻辑与
template<class T> bool logic_or<T> //逻辑或
template<class T> bool logic_not<T> //逻辑非
*/
void test_func__05()
{
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;
//利用逻辑非 将容器v 搬运到 容器v2中,并执行取反操作
vector<bool>v2;
v2.resize(v.size());//要先开辟空间才能搬运
//搬运
transform(v.begin(), v.end(), v2.begin(), logical_not<bool>()); //logical_not<bool>() 小括号()代表对象的创建
for (vector<bool>::iterator it = v2.begin(); it != v2.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
/*STL常用算法
算法主要是由头文件<algorithm> <functional> <numeric> 组成
<algorithm> 是STL头文件中最大的一个,范围涉及比较,交换,查找,遍历操作,复制,修改等等
<numeric> 体积很小,只包括几个在序列上面进行简单数学运算的模板函数
<functional>定义了一些模板类,用以声明函数对象
*/
/*常用遍历算法
for_each //遍历容器
transform //搬运容器到另一个容器
*/
/* 实际开发最常用
for_each(iterator beg ,iterator end , _func); // _func 函数或函数对象
*/
//普通函数
void print01(int val)
{
cout << val << " ";
}
//仿函数
class print02 {
public:
void operator()(int val)
{
cout << val << " ";
}
};
void test_arithmetic_01()
{
vector<int>v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
//1.普通函数
for_each(v.begin(),v.end(),print01); //print01普通函数 函数名
cout << endl;
//2.仿函数
for_each(v.begin(), v.end(), print02()); //print02()仿函数函数
cout << endl;
}
/*
transform(iterator beg1,iterator end1,iterator beg2,_func); //源容器开始迭代器 ,结束迭代器 ,目标容器开始迭代器 ,函数或目标函数
*/
class Transform {
public:
int operator()(int val)
{
return val + 100;//可以搬运过程中改数据
}
};
void test_arithmetic_02()
{
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(), print01);
cout << endl;
}
/*常用查收算法
find //查找算法
find_if //按条件查找元素
adjacent_find //查找相邻重复元素
binary_search //二分查找法
count //统计元素个数
count_if //按条件统计元素个数
*/
int main()
{
//cout << << endl;
test_arithmetic_02();
}