C++模板技术和STL实战开发(9)——STL容器与算法(3)——stack、queue、bitset应用案例

1.stack案例:生成固定大小的栈

代码实现:

#include <iostream>
#include <stack>
#include <deque>

using namespace std;

template<typename  T,typename Container=deque<T>>
class MyStack :public stack<T,Container>
{
public:
	MyStack(size_t size)
		:maxSize(size)
	{

	}

	void push(const T& t)
	{
		if (stack<T,Container>::size() < maxSize)
		{
			stack<T,Container>::push(t);
		}
		else
		{
			cout << "栈空间已满," << t << "未能进栈" << endl;
		}
	}
private:
	size_t maxSize;   //栈的大小
};

int main()
{
	MyStack<int, deque<int>> stackObj(2);
	stackObj.push(1);
	stackObj.push(2);
	stackObj.push(3);
}

测试结果:

2.priority_queue案例:打印学生成绩

需求:

显示学生成绩:总成绩由高到低,当总成绩相同的时候,数学成绩高的优先

代码实现:

#include <iostream>
#include <string>
#include <queue>
using namespace std;

class Student
{
public:
	Student(string name, int Chinese, int Math)
		:stu_name(name), grade_Chinese(Chinese), grade_Math(Math)
	{
	}

	string getName ()const
	{
		return stu_name;
	}
	int getChinese ()const
	{
		return grade_Chinese;
	}
	int getMath ()const
	{
		return grade_Math;
	}
	bool operator<(const Student& s)const
	{
		int sum1 = grade_Chinese + grade_Math;
		int chinese2 = s.getChinese();
		int math2 = s.getMath();
		int sum2 = chinese2 + math2;
		if (sum1 < sum2)
			return true;
		if (sum1 == sum2 && (grade_Math < math2))
			return true;
		return false;
	}
private:
	string stu_name;
	int grade_Chinese;
	int grade_Math;
};

//用容器来整理数据
int main()
{
	Student s[] = {
					Student("Lucy",70,80),
					Student("Lily",60,80),
					Student("Mark",80,70),
					Student("LiMing",90,60),
	};

	priority_queue<Student> pr(s,s+4);
	cout << "姓名\t数学\t语文" << endl;
	while (!pr.empty())
	{
		const Student& t = pr.top();
		cout << t.getName() << "\t" << t.getMath() << "\t" << t.getChinese() << endl;
		pr.pop();
	}
}

测试:

3.bitset案例:二进制数取反、故障统计

由于C语言没有固定的二进制表示法,我们在通信层做C/C++的混合编程时,bitset有广泛应用

案例1:二进制数取反

一个8位二进制,要求高4位不变,低4位取反

代码实现:

#include <iostream>
#include <string>
#include <bitset>
using namespace std;


int main()
{
	bitset<8> b(string("11010011"));
	cout << "原数据:" << b.to_string() << endl;
	for (int i = 0; i < 4; i++)
	{
		b.flip(i);
	}
	cout << "新数据1:" << b.to_string() << endl;

	bitset<8> b2(string("00001111"));
	b ^= b2;
	cout << "新数据2:" << b.to_string() << endl;
}

案例2:故障统计

需求:

故障统计:假定统计一个设备每月处故障的情况,31天用31位来表示,如果出故障是1,没有故障是0(总共不到4个字节,大大节省了空间)

代码实现:

#include <iostream>
#include <string>
#include <bitset>
#include <vector>
using namespace std;

template<size_t N>
class MyAttend
{
public:
	MyAttend(int month, string strAttend)
		:m_month(month), m_b(strAttend)
	{

	}
	int getMonth()
	{
		return m_month;
	}

	int getAttendDays()
	{
		return m_b.count();
	}


private:
	int m_month;
	bitset<N> m_b;  //出故障的位容器
};

class Dervice
{
public:
	Dervice(string name)
		:m_name(name)
	{
	}

	void Add(MyAttend<31>& m)
	{
		v.push_back(m);
	}

	void show()
	{
		cout << "设备名 " << m_name << endl;
		cout << "月份\t故障上报天数 " << endl;
		for (size_t i = 0; i < v.size(); i++)
		{
			MyAttend<31>& m = v.at(i);
			int month = m.getMonth();
			int days = m.getAttendDays();
			cout << month << "\t" << days << endl;
		}
	}
private:
	string m_name;
	vector<MyAttend<31>> v;
};
int main()
{
	Dervice d1("加工中心");
	string s1 = "11111110001011101000100110";
	string s2 = "11001110111011101110101100";

	MyAttend<31> m1(1, s1);
	MyAttend<31> m2(2, s2);
	d1.Add(m1);
	d1.Add(m2);
	d1.show();
}

测试结果:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值