C++自学笔记

本文介绍了C++中使用multimap进行员工分组的案例,包括员工信息存储、随机分组及分组显示。同时,详细阐述了STL中的函数对象,包括概念、使用方式、谓词(一元谓词、二元谓词)、内建函数对象(算术、关系、逻辑)。通过示例代码展示了如何自定义和使用函数对象进行操作。
摘要由CSDN通过智能技术生成

33 案例 — 员工分组

案例描述:

  • 公司招聘了10个员工(ABCDEFGHIJ),10名员工进入公司之后,指派员工在哪个部门工作。
  • 员工信息有:姓名 工资组成;部分分为:策划、美术、研发。
  • 随机给10名员工分配部分和工资。
  • 通过multimap进行信息的插入,key(部门编号) value(员工)。
  • 分部门显示员工信息。

实现步骤:

1、创建10名员工,放到vector中;
2、遍历vector容器,取出每个员工,进行随机分组;
3、分组后,将员工部门编号作为key,具体员工 作为value,放到multimap容器中;
4、分组显示员工信息。

#include<iostream>
#include <stdlib.h>
#include<string>
using namespace std;
#include<vector>
#include<map>
#include<ctime>

//部门定义
#define CEHUA 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; //10000~19999
		v.push_back(worker);
	}
}

void printVector(vector<Worker>& v)
{
	for (vector<Worker>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << "姓名: " << it->m_Name << "\t" 
			<< "工资: " << it->m_Salary << "\t" << endl;
	}
	cout << endl;
}

//分组
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 printMap(multimap<int, Worker>& m)
{
	for (multimap<int, Worker>::iterator it = m.begin(); it != m.end(); it++)
	{
		cout << "部门编号:" << it->first << "\t" << "姓名:" << it->second.m_Name << "\t"
			<< "工资:" << it->second.m_Salary << "\t" << endl;
	}
	cout << endl;
}

//分组显示员工
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 << "姓名:" << pos->second.m_Name << "\t"
			<< "工资:" << pos->second.m_Salary << "\t" << 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 << "\t"
			<< "工资:" << pos->second.m_Salary << "\t" << 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 << "\t"
			<< "工资:" << pos->second.m_Salary << "\t" << endl;
	}
}


int main()
{
	srand((unsigned int)time(NULL));

	vector<Worker>vWorker;
	//创建员工
	createWorker(vWorker);
	//printVector(vWorker);

	//分组
	multimap<int, Worker>mWorker;
	setGroup(vWorker, mWorker);
	//printMap(mWorker);

	//分组显示员工
	showWorkerByGroup(mWorker);


	system("pause");

	return 0;
}

34 STL - 函数对象

本次记录STL - 函数对象,还请各位大佬批评指正!

函数对象概念

概念:

  • 重载函数调用操作符的类,其对象常称为函数对象;
  • 函数对象使用重载的 () 时,行为类似函数调用,也叫仿函数

本质: 函数对象(仿函数)是一个类,不是一个函数。

函数对象的使用

特点:

  • 函数对象在使用时,可以像普通函数那样调用,可以有参数,可以有返回值;
  • 函数对象超出普通函数的概念,函数对象可以有自己的状态;
  • 函数对象可以作为参数传递。
#include<iostream>
#include <stdlib.h>
#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 s)
	{
		cout << s << endl;
		count++;
	}

	int count; //内部自己的状态
};
void test02()
{
	MyPrint myprint;
	myprint("Hello World!");
	myprint("Hello World!");
	myprint("Hello World!");
	cout << "MyPrint 调用次数为:" << myprint.count << endl;
}

//函数对象可以作为参数传递。
void doPrint(MyPrint& mp, string s)
{
	mp(s);
}
void test03()
{
	MyPrint myprint;
	doPrint(myprint, "Hello C++!");
}

int main()
{
	test01();
	test02();
	test03();
	
	system("pause");

	return 0;
}

谓词

谓词概念:

返回bool类型的仿函数称为谓词;
如果operator()接收一个参数,那么叫做一元谓词;
如果operator()接收两个参数,那么叫做二元谓词。

一元谓词

#include<iostream>
#include <stdlib.h>
#include<string>
using namespace std;
#include<vector>
#include<algorithm>


//一元谓词
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的数字,find_if 返回的是迭代器
	//GreaterFive() 匿名的函数对象
	vector<int>::iterator it = find_if(v.begin(), v.end(), GreaterFive());
	if (it == v.end())
	{
		cout << "未找到大于5的数字!" << endl;
	}
	else
	{
		cout << "找到了大于5的数字为:" << *it << endl;
	}
}

int main()
{
	test01();

	
	system("pause");

	return 0;
}

二元谓词

#include<iostream>
#include <stdlib.h>
#include<string>
using namespace std;
#include<vector>
#include<algorithm>


//二元谓词
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 v1,int v2)
	{
		return v1 > v2;
	}
};

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);

	cout << "排序(默认升序):" << endl;
	sort(v.begin(), v.end());
	printVector(v);

	//使用函数对象,改变算法策略,变为排序规则从大到小
	cout << "降序排序:" << endl;
	sort(v.begin(), v.end(), MyCompare());//匿名函数对象
	printVector(v);
}

int main()
{
	test01();

	
	system("pause");

	return 0;
}

内建函数对象

概念: 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 divides< T > //除法仿函数
  • template< class T > T modulus< T > //取余仿函数
  • template< class T > T negate< T > //取反仿函数
#include<iostream>
#include <stdlib.h>
#include<string>
using namespace std;
#include<functional>


//内建函数对象 算术仿函数
//negate 一元仿函数 取反仿函数
void test01()
{
	negate<int>n;
	cout << n(50) << endl;
}

//plus 二元仿函数 加法
void test02()
{
	plus<int>p;
	cout << p(10, 20) << endl;
}

//减 乘 除 取余
void test03()
{
	minus<int>m;
	cout << "减:" << m(20, 10) << endl; //20-10

	multiplies<int>mu;
	cout << "乘:" << mu(20, 10) << endl;

	divides<int>d;
	cout << "除:" << d(20, 10) << endl; // 20/10

	modulus<int>mo;
	cout << "取余:" << mo(20, 11) << endl; 
}
int main()
{
	test01();
	test02();
	test03();
	
	system("pause");

	return 0;
}

关系仿函数

功能描述: 实现关系对比。

仿函数原型:

  • template< class T > bool equal_to< T > //等于
  • template< class T > bool not_equal_to< T > //不等于
  • template< class T > bool greater< T > //大于 最常用
  • template< class T > bool greater_equal< T > //大于等于
  • template< class T > bool less< T > //小于
  • template< class T > bool less_equal< T > //小于等于
#include<iostream>
#include <stdlib.h>
#include<string>
using namespace std;
#include<vector>
#include<algorithm>
#include<functional>


//关系仿函数
//大于 greater
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 v1, int v2)
	{
		return v1 > v2;
	}
};

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);

	cout << "无序:" << endl;
	printVector(v);

	//升序
	cout << "升序:" << endl;
	sort(v.begin(), v.end());
	printVector(v);

	//降序
	cout << "降序:" << endl;
	//sort(v.begin(), v.end(), MyCompare());
	//greater<int>() 内建函数对象
	//sort 默认为less
	sort(v.begin(), v.end(), greater<int>());
	printVector(v);
}

int main()
{
	test01();
	
	system("pause");

	return 0;
}

逻辑仿函数

功能描述: 实现逻辑运算。
实际应用较少,了解即可。

仿函数原型:

  • template< class T > bool logical_and< T > //等于
  • template< class T > bool logical_or< T > //不等于
  • template< class T > bool logical_not< T > //大于 最常用
#include<iostream>
#include <stdlib.h>
#include<string>
using namespace std;
#include<vector>
#include<algorithm>
#include<functional> //内建函数对象头文件


//逻辑仿函数
//逻辑非 logical_not
void printVector(vector<bool>& v)
{
	for (vector<bool>::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);

	printVector(v);

	//利用逻辑非将容器 v 搬运到容器 v2 中,并执行取反操作
	vector<bool>v2;
	v2.resize(v.size());

	transform(v.begin(), v.end(), v2.begin(), logical_not<bool>());
	printVector(v2);
}

int main()
{
	test01();
	
	system("pause");

	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值