STL【四】
1、STL案例练习----员工分组
案例描述
- 公司今天招聘了10个员工(ABCDEFGHIJ),10名员工进入公司后,需要指派员工在哪个部门工作
- 员工信息有:姓名、工资组成;部门分为:策划、美术、研发
- 随机给10名员工分配部门和工资
- 通过Multimap进行信息的插入,
- 分部门显示员工信息
步骤实现
- 创建10名员工放到vector中
- 遍历vector容器,取出每个员工进行随机分组
- 分组后,将员工部门编号作为key,具体员工作为value,放入到multimap容器中
- 分部门显示员工信息
代码实现
#include <iostream>
#include <map>
#include <vector>
#include <string>
#include <ctime>
using namespace std;
#define CEHUA 0
#define MEISHU 1
#define YANFA 2
//员工类
class Worker
{
public:
string m_name;//姓名
int m_salary;//薪资
};
void creatWorker(vector<Worker>& v)
{
string nameseed = "ABCDEFGHIJ";
for (int i = 0; i < 10; i++)
{
Worker worker;
worker.m_name = "员工";
worker.m_name += nameseed[i];
worker.m_salary = rand() % 10000 + 10000;//10000-9999
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
m.insert(make_pair(deptId, *it));
}
}
void showWorkerByGroup(multimap<int, Worker>& m)
{
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 << "name:" << pos->second.m_name << " " << "salary:" << pos->second.m_salary << endl;
}
cout << endl;
cout << "***************************************" << endl;
cout << "美术部门信息:" << endl;
pos = m.find(MEISHU);
count = m.count(MEISHU);//统计美术部门具体人数
index = 0;
for (; pos != m.end() && index < count; pos++,index++)
{
cout << "name:" << pos->second.m_name << " " << "salary:" << pos->second.m_salary << endl;
}
cout << endl;
cout << "***************************************" << endl;
cout << "研发部门信息:" << endl;
pos = m.find(YANFA);
count = m.count(YANFA);//统计研发部门具体人数
index = 0;
for (; pos != m.end() && index < count; pos++,index++)
{
cout << "name:" << pos->second.m_name << " " << "salary:" << pos->second.m_salary <<endl;
}
}
int main()
{
srand((unsigned int)time(NULL));
//创建员工
vector<Worker> vworker;
creatWorker(vworker);
/*for (vector<Worker>::iterator it = vworker.begin(); it != vworker.end(); it++)
{
cout << "name:" << it->m_name << " " << "salary:" << it->m_salary << " ";
}
cout << endl;*/
//分组
multimap<int, Worker> mworker;
setGroup(vworker,mworker);
//分组显示员工
showWorkerByGroup(mworker);
return 0;
}

2、函数对象
💖概念:
- 重载函数调用操作符的类,其对象常称为函数对象
- 函数对象使用重载的()时,行为类似函数调用,也叫仿函数
💖本质:
- 函数对象(仿函数)是一个类,不是一个函数
💖函数对象使用特点:
- 函数对象在使用时,可以像普通函数那样调用,可以有参数,可以有返回值
- 函数对象超出普通函数的概念,函数对象可以有自己的状态
- 函数对象可以作为参数传递
示例代码:
#include <iostream>
#include <string>
using namespace std;
//函数对象(仿函数)
//函数对象在使用时,可以像普通函数那样调用,可以有参数,可以有返回值
class MyAdd
{
public:
int operator()(int v1, int v2)
{
return v1 + v2;
}
};
void test01()
{
MyAdd myAdd;
cout << myAdd(10, 10) << endl;
}
//函数对象超出普通函数的概念,函数对象可以有自己的状态
class MyPrint
{
public:
MyPrint()
{
this->count = 0;
}
//统计该函数调用多少次
void operator()(string test)
{
cout << test << endl;
this->count++;
}
//内部直接加状态
int count;
};
void test02()
{
MyPrint mPrint;
mPrint("hello world");
mPrint("hello world");
mPrint("hello world");
mPrint("hello world");
cout << "调用次数:" << mPrint.count << endl;
}
//函数对象可以作为参数传递
void doPrint(MyPrint& mp, string test)
{
mp(test);
}
void test03()
{
MyPrint mPrint;
doPrint(mPrint, "hello worldC++");
}
int main()
{
test01();
test02();
test03();
return 0;
}

3、谓词
💖概念:
- 返回bool类型的仿函数称为谓词
- 如果operator接受一个参数,那么叫做一元谓词
- 如果operator接受两个参数,那么叫做二元谓词
示例代码:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
class GreatFive
{
public:
bool operator()(int val)
{
return val > 5;
}
};
//一元谓词练习
void test01()
{
vector<int> v;
for (int i = 0; i < 10; i++)
{
v.push_back(i + 1);
}
//查找有没有大于5的数字
vector<int>::iterator it = find_if(v.begin(), v.end(), GreatFive());//GreatFive()----匿名函数对象
if (it == v.end())
{
cout << "not find > 5" << endl;
}
else
{
cout << "find it" << " " << *it << endl;
}
}
//二元谓词练习
class mySort
{
public:
bool operator()(int v1, int v2)
{
return v1 > v2;
}
};
void test02()
{
vector<int> v;
v.push_back(10000);
for (int i = 0; i < 5; i++)
{
v.push_back(i + 1);
}
v.push_back(991);
cout << "排序前:" << endl;
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
sort(v.begin(), v.end());
cout << "升序:" << endl;
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
//使用函数对象改变排序规则为降序
sort(v.begin(), v.end(), mySort());
cout << "降序:" << endl;
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
int main()
{
test01();
test02();
return 0;
}

4、内建函数对象
💖概念:
- STL内建了一些函数对象
💖分类:
- 算术仿函数
- 关系仿函数
- 逻辑仿函数
💖用法:
- 这些仿函数所产生的对象,用法和一般函数完全相同
- 使用内建函数对象,需要引入头文件
#include <functional>
4.1算术仿函数
- 实现加减乘除取模等运算
练习代码:
#include <iostream>
#include <functional>
using namespace std;
//算术仿函数
//negate 一元仿函数 取反仿函数
void test01()
{
negate<int> n;
cout << n(30) << endl;
}
//plus 二元仿函数 加法
void test02()
{
plus<int> p;
cout << p(10, 30) << endl;
}
int main()
{
test01();
test02();
return 0;
}

4.2关系仿函数
- 实现关系对比
练习代码:
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
//大于 greater
void test01()
{
vector<int> v;
v.push_back(1);
v.push_back(3);
v.push_back(4);
v.push_back(2);
v.push_back(0);
v.push_back(7);
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
//使用内建仿函数
sort(v.begin(), v.end(), greater<int>());
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
int main()
{
test01();
return 0;
}

4.3逻辑仿函数
- 实现逻辑运算
练习代码:
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
//逻辑非 logical_not 练习
void test01()
{
vector<bool> v;
v.push_back(true);
v.push_back(false);
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>());
for (vector<bool>::iterator it = v2.begin(); it != v2.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
int main()
{
test01();
return 0;
}

🐱👓🐱👓🐱👓
STL案例与函数对象
本文介绍了使用STL解决实际问题的案例,如员工分组,并深入探讨了函数对象、谓词及内置函数对象的应用。通过具体代码示例,展示了如何高效地利用STL进行编程。
1286

被折叠的 条评论
为什么被折叠?



