#include
using namespace std;
#include
#include
#define CheHua 0
#define MeiShu 1
#define YanFa 2
class Worker
{
public:
string m_Name;
int m_Salary;
};
void createWorker(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; //随机分配对应员工的工资
//将员工放入容器中
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 showWorkerByGourp(multimap<int, Worker>& m)
{
cout << "策划部门:" << endl;
//查找对应部门
multimap<int, Worker>::iterator pos = m.find(CheHua);
int count = m.count(CheHua); //统计具体人数
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;
}
}
int main()
{
srand((unsigned int)time(NULL)); //随机初始化种子
//1、创建Worker类的员工
vector<Worker>vWorker;
createWorker(vWorker);
//2、分组容器
multimap<int, Worker>mWorker;
setGroup(vWorker, mWorker);
//分组显示
showWorkerByGourp(mWorker);
测试
//for (vector::iterator it = vWorker.begin(); it != vWorker.end(); it++)
//{
// cout << "姓名:" << it->m_Name << " 工资" << it->m_Salary << endl;
//}
system("pause");
return 0;
}
概念:
本质:函数对象(仿函数)是一个类,不是一个函数。
#include
//函数对象(仿函数)
//重载函数调用操作符的类,其对象常称为函数对象
//函数对象使用重载的()时,行为类似函数调用,也叫仿函数
//本质:函数对象(仿函数)是一个类,不是一个函数
class MyAdd
{
public:
int operator()(int v1, int v2){
return v1 + v2;
}
};
//1、函数对象在使用时,可以像普通函数那样调用, 可以有参数,可以有返回值
void test01()
{
MyAdd myAdd; //创建出myAdd的函数对象
cout << myAdd(10, 10) << endl; //重载了小括号()
}
//2、函数对象超出普通函数的概念,函数对象可以有自己的状态
class MyPrint
{
public:
MyPrint(){
this->count = 0;
}
void operator()(string test){
cout << test << endl;
count++;
}
int count; //内部自己的状态
};
void test02()
{
MyPrint myPrint;
myPrint("hello world"); //重载了小括号()
myPrint("hello world");
myPrint("hello world");
myPrint("hello world");
cout << "函数调用的次数为: " << myPrint.count << endl;
}
//3、函数对象可以作为参数传递
void doPrint(MyPrint& mp, string test)
{
mp(test); //重载了小括号() 类似于myPrint(test)即myPrint("Hello C++")
cout << "count = " << mp.count << endl;
}
void test03()
{
MyPrint myPrint;
doPrint(myPrint, "Hello C++");
}
int main()
{
//test01();
//test02();
test03();
}
概念:
#include
//仿函数 返回值类型是bool数据类型,称为谓词
//一元谓词 operator()接受一个参数
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);
}
//查找容器中 有没有大于5的数字
//GreaterFive() 匿名函数对象
//find_if函数 按条件方式找一找有没有想要的结果
vector<int>::iterator it = find_if(v.begin(), v.end(), GreaterFive());
if (it == v.end()){
cout << "未找到" << endl;
}
else{
cout << "找到了大于5的数字为: " << *it << endl;
}
}
int main()
{
test01();
}
#include
#include
//二元谓词 operator()接受两个参数
void printVector(vector<int>& v)
{
for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
cout << *it << " ";
}
cout << endl;
}
class MyCompare
{
public:
bool operator()(int val1, int val2){
return val1 > val2;
}
};
void test01()
{
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());
printVector(v);
//使用函数对象 改变算法策略,变为排序规则从大到小
sort(v.begin(), v.end(), MyCompare());
printVector(v);
}
int main()
{
test01();
}
概念:STL内建了一些函数对象
分类:
用法:
#include
功能描述:实现四则运算,其中negate是一元运算,其他都是二元运算。
仿函数原型:
template T plus
//加法仿函数template T minus
//减法仿函数template T multiplies
//乘法仿函数template T divides
//除法仿函数template T modulus
//取模仿函数template T negate
//取反仿函数#include //内建函数对象头文件
//内建函数对象 算术仿函数
//negate 一元仿函数 取反仿函数
void test01()
{
negate<int> n;
cout<< n(50) << endl;
}
//puls 二元仿函数 加法
void test02()
{
plus<int> p;
cout << p(10, 20) << endl;
}
int main()
{
//test01();
test02();
}
仿函数原型:
template bool equal_to
//等于template bool not_equal_to
//不等于template bool greater
//大于template bool greater_equal
//大于等于template bool less
//小于template bool less_equal
//小于等于#include
#include
#include
//内建函数对象 关系仿函数
//大于 greater
class MyCompare
{
public:
bool operator()(int val1, int val2){
return val1 > val2;
}
};
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;
}
int main()
{
test01();
}
函数原型:
template bool logical_and
//逻辑与template bool logical_or
//逻辑或template bool logical_not
//逻辑非#include
#include
#include
//内建函数对象 逻辑仿函数
//逻辑非 logical_not
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;
//利用逻辑非 将容器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();
}