c++机房预约系统部分理解

需求

请添加图片描述
请添加图片描述

理解

  1. 模板优化代码量
    背景:
    有些代码,比如管理员的查看信息功能,我想优化成用模板,可以减少代码。但是发现在输出的时候,学生和老师统一性就更高了,导致输出提示只能是id号,而不是学号、职工号。 还有就是,我用模板前,本想用identity来放在输出函数里面,输出信息,但是想了下,这样会改变类的结构,因为id号只有学生和老师有,管理员没得,该了化其他地方可能会出问题,牵一发而动全身,划不来。
    理解:
    改变了需求,所以在开发过程中,做一些优化结构但又不是那么必要的优化时候,要考虑多个方面,最好是不做不必要的优化。
//输出模板
template<class T>
void printPerson(T& t)
{
	cout << "id号(学号/职工号):  " << t.id << "\t\t用户名:" << t.m_Name << "\t\t用户密码:" << t.m_Psw << endl;
}
//查看账号
void Manager::showPerson()
{
	cout << "请选择查看内容:" << endl;
	cout << "1.查看所有学生" << endl;
	cout << "2.查看所有老师" << endl;

	int select;

	cin >> select;

	if (select == 1)
	{
		cout << "\n所有的学生信息如下:\n\n";
		for_each(vStu.begin(), vStu.end(), printPerson<Student>);
	}
	else
	{
		cout << "\n所有的老师信息如下:\n\n";
		for_each(vTea.begin(), vTea.end(), printPerson<Teacher>);
	}
	system("pause");
	system("cls");
}
  1. 压入容器的对象和源对象:
    背景:
    将一个对象压入vector后,改变这个对象的属性,vector会改变不?
    理解:
    如图,vector的t,和原来t不是一个东西
    在这里插入图片描述
#include <iostream>
#include<vector>
#include<algorithm>
using namespace std;
 
class Temp
{
public:
	Temp(int a, int b)
	{
		this->a = a;
		this->b = b;
	}
	
 	int a;
 	int b;
};

void test()
{
    Temp t(1,2);
    cout << "t :" << t.a << endl;
    
    vector<Temp>v;
    v.push_back(t);
    
    t.a = 3;
    
    cout << "new t: " << t.a << endl;
    cout << "vector t: " << v[0].a << endl;   
}
 
int main()
{
    test();
    return 0;
}
  1. c++字符串转化 数
    用 atoi(),先转化c风格字符,再转化int
atoi( of.m_oderData[i]["stuID"].c_str() )

疑惑

为什么被注释的可以在管理员界面输出vector里面的值,而没注释的输出不了?

	/*Student stu;
	while (ifs >> stu.id && ifs >> stu.m_Name && ifs >> stu.m_Psw)
	{
		vStu.push_back(stu);
	}*/
	//为什么不可以?
	Student stu;
	Student* s2;
	while (ifs >> stu.id && ifs >> stu.m_Name && ifs >> stu.m_Psw)
	{
		s2 = new Student (stu.id, stu.m_Name, stu.m_Psw);
		vStu.push_back(*s2);
	}

另外测试了一下以下代码,却又可以输出

#include <iostream>
#include<vector>
#include<algorithm>
using namespace std;
 

 
class Temp
{
public:
	Temp(int a, int b)
	{
		this->a = a;
		this->b = b;
	}
	
 	int a;
 	int b;
};
 
  class MyPrint
  {
  	public:
  		void operator() (Temp t)
  		{
 		 	cout << "值:" << t.a << endl;
 		 }
  };
 
void test()
{
    Temp * t1;
    vector<Temp> vt;
    
    int i = 5;
    while(i--)
    {
		t1 = new Temp(i, i*2);
		vt.push_back(*t1);
	}
	
	for_each(vt.begin(), vt.end(), MyPrint());
}
 
int main()
{
    test();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Dr.勿忘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值