关于c++中的vector

           今天在c++中写了一个职工管理的小项目,主要运用了vector的相关知识,vector是C++标准模板库中的部分内 容,简单地说,vector是个能够存放任意类型的动态数组,能够增加和压缩数据。首先在程序开头处加上#include<vector>以包含所需要的类文件vector还有一定要加上using namespace std;它可以像普通数组一样访问,可以顺序地向容器中填充数据,还可以动态地改变它的大小,也可以在容器中装入自定义的数据类型,可以把一个容器的对象赋值给另外一个容器。对于vector 的数据的存入和输出可以用v.begin()和v.end() 来得到vector开始的和结束的元素地址的指针位置。为了方便理解,举一个例子:vector <int *> a  int b = 5; a.push_back(b);//在数组的最后添加一个数据  cout<<a[0]; //最后输出的结果为5 。

下面是自己写的这个项目的代码:

文件名:people.h

#ifndef PEOPLE_H
#define PEOPLE_H

#include <string>
using namespace std;

class People
{
public:
	string name; //姓名
	string age;  //年龄
	string sex;  //性别
	string id;   //职工号
public:
	People();
	People(string,string,string,string);
};

文件名:worker.h

#ifndef WORKER_H
#define WORKER_H

#include "People.h"
#include <string>
using namespace std;

class Worker :public People
{
public:
	string mail;     //邮编
	string section ; //部门
	double salary;   //薪资
public:
	Worker();
	Worker(string,string,string,string,string,string,double); 
};

#endif

文件名:plan.h

#ifndef  PLAN_H
#define  PLAN_H

#include "worker.h"
#include <functional>
#include <algorithm>

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

class Plan
{
public:
	Plan();
	void jiemian();                          //主菜单界面
	void find(std::vector<Worker> &ver);    //查询职工信息
	void edit(std::vector<Worker> &ver);    //修改职工信息
	void del(std::vector<Worker> &ver);     //删除职工信息
	void insert(std::vector<Worker> &ver);  //添加职工信息
	void st(std::vector<Worker>&ver);       //按职工薪资查询排名
	void list(std::vector<Worker>&ver);     //浏览职工信息
};

#endif

文件名:main.cpp

#include "plan.h"
#include <vector>
#include <iostream>
using namespace std; 

int main()
{
	int temp = 1 ;
	char flag;
	vector<Worker> vecobj;
	Plan con;
	while(temp == 1)
	{
		con.jiemian(); //显示界面
		cin>>flag;
		cout<<endl;
		switch(flag)
		{
		case '1':
			con.insert(vecobj);
			break;
		case '2':
			con.del(vecobj);
			break;
		case '3':
			con.edit(vecobj);
			break;
		case '4':
			con.find(vecobj);
			break;
		case '5':
			con.list(vecobj);
			break;	
		case '6':
			con.st(vecobj);
			break;
		case '0':
			temp=0;
			break ;
		}
	}
	return 0 ;
}
文件名:people.cpp

#include "People.h"
#include <string>
using namespace std;

People::People()
{}
People::People(string m_name,string m_age,string m_sex,string m_id)
{
	name=m_name;
	age=m_age;
	sex=m_sex;
	id=m_id;
}
文件名:worker.cpp

#include "worker.h"
#include <string>
using namespace std;

Worker::Worker()
{}
Worker::Worker(string m_name,string m_age,string m_sex,string m_id,string mymail,string mysection,double mysalary):People(m_name,m_age,m_sex,m_id)
{
	mail=mymail;
	section=mysection;
	salary=mysalary;
}

文件名:plan.cpp

#include "plan.h"
#include<windows.h>
#include <string>
#include <iostream>
#include <vector>
#include <functional>
#include <algorithm>
#include <time.h>
using namespace std;
char* mytime()
{
	time_t rawtime;
	struct tm * timeinfo;
	time ( &rawtime );
	timeinfo = localtime ( &rawtime );
	return asctime (timeinfo);	
}

bool mysalary(Worker& wor1, Worker& wor2) //比较两名职工薪资的大小
{
	return wor1.salary<wor2.salary;
}


Plan::Plan()
{}
void Plan::jiemian()       //主菜单界面
{
	system("cls") ;	
	system("color 5F");
	cout<<endl;
	cout<<endl;
	cout<<"\t\t****************************************************"<<endl;
	cout<<"\t\t*   "<<mytime() ;                                                 
	cout<<"\t\t*        请选择您需要的操作!                      *"<<endl;
	cout<<"\t\t*                         (1)添加职工人员          *"<<endl;
	cout<<"\t\t*                         (2)删除职工人员          *"<<endl;
	cout<<"\t\t*                         (3)修改职工信息          *"<<endl;	
	cout<<"\t\t*                         (4)查询                  *"<<endl;
	cout<<"\t\t*                         (5)显示所有职工个人信息  *"<<endl;
	cout<<"\t\t*                         (6)按薪资排名            *"<<endl;
	cout<<"\t\t*                         (0)退出                  *"<<endl;	
	cout<<"\t\t*       选择相对的括号里的阿拉伯数字!              *"<<endl;
	cout<<"\t\t****************************************************";
	cout<<endl;
	cout<<endl;
	return;
}
void Plan::find(std::vector<Worker> &ver)     //查询职工信息
{
	system("cls");
	string stucode;
	string stuname;
	cout<<"请输入职工工号:";
	cin>>stucode;
	cout<<endl ;
	cout<<"请输入职工姓名";
	cin>>stuname;
	cout<<endl ;
	vector<Worker>::iterator i;
	for(i=ver.begin();  i!=ver.end(); i++)
	{
		if( ((*i).id==stucode)&&((*i).name==stuname) )
		{
			cout<<"职工号:"<<(*i).id<<"职工姓名:"<<(*i).name<<" 职工年龄:"<<(*i).age<<" 性别:"<<(*i).sex<<" 邮编:"<<(*i).mail
				<<" 部门:"<<(*i).section<<" 薪资:"<<(*i).salary<<endl;		
			
		}
		else
		{
			cout<<"对不起 未找到您所要查询的职工信息";
		}
		Sleep(4000) ;		
	}
	return;
}
void Plan::list(std::vector<Worker>&ver)  //浏览职工个人信息
{
	system("cls");
	vector<Worker>::iterator  i;
	for(i=ver.begin(); i!=ver.end(); i++)
	{
		cout<<"职工号:"<<(*i).id<<"职工姓名:"<<(*i).name<<" 职工年龄:"<<(*i).age<<" 性别:"<<(*i).sex<<" 邮编:"<<(*i).mail
			<<" 部门:"<<(*i).section<<" 薪资:"<<(*i).salary<<endl;	
	}
	cout<<endl<<endl<<endl;
	string name;
    cout<<"按任意键返回主界面:";
	cin>>name ;

	return;
}
void Plan::edit(std::vector<Worker> &ver)   //修改职工信息
{
	system("cls");
	string stucode;
	string stuname;
	cout<<"请输入职工工号:";
	cin>>stucode;
	cout<<endl ;
	cout<<"请输入职工姓名";
	cin>>stuname;
	cout<<endl ;
	vector<Worker>::iterator i;
	for(i=ver.begin();  i!=ver.end(); ++i)
	{
		if( ((*i).id==stucode)&&((*i).name==stuname))
		{	
			cout<<"原来的信息:"<<endl;
			cout<<endl ;
			cout<<"职工号:"<<(*i).id<<"职工姓名:"<<(*i).name<<" 职工年龄:"<<(*i).age<<" 性别:"<<(*i).sex<<" 邮编:"<<(*i).mail
				<<" 部门:"<<(*i).section<<" 薪资:"<<(*i).salary<<endl;	
			cout<<"请输入新信息:"<<endl;
			cout<<endl ;
			cout<<"请输入姓名:";
			cin>>(*i).name;
			cout<<endl ;
			cout<<"请输入年龄:";
			cin>>(*i).age;
			cout<<endl ;
			cout<<"请输入性别:";
			cin>>(*i).sex;
			cout<<endl ;
			cout<<"请输入职工号:";
			cin>>(*i).id;
			cout<<endl ;
			cout<<"请输入职工邮编:";
			cin>>(*i).mail;
			cout<<endl ;
			cout<<"请输入职工部门:";
			cin>>(*i).section;
			cout<<endl ;
			cout<<"请输入职工薪资:";
			cin>>(*i).salary;
			cout<<endl ;
			cout<<"系统正在存储信息,即将返回主界面......"<<endl;
			Sleep(3000) ;
		}
		else
		{
			cout<<"对不起 未找到您所要查询的职工信息";
			
		}
		return;
	}

}
void Plan::del(std::vector<Worker> &ver)    //删除职工信息
{
	system("cls");
	string stucode;
	string stuname;
	cout<<"请输入职工工号:";
	cin>>stucode;
	cout<<endl ;
	cout<<"请输入职工姓名";
	cin>>stuname;
	cout<<endl ;
	vector<Worker>::iterator i;
	for(i=ver.begin();  i!=ver.end(); ++i)
	{
		if( ((*i).id==stucode)&&((*i).name==stuname))
		{
			ver.erase(i);                   //erase是C++中String类库中提供的一个函数
			return;	   
		}
	}
	cout<<"对不起 未找到您所要查询的职工!"<<endl;
	Sleep(2000) ;
	return;
}
void Plan::insert(std::vector<Worker> &ver)    //添加职工信息
{
	system("cls");
	Worker wor;
	string stucode;
	string stuname;
	string stusex;
	string stuage;
	string stumail;
	string stusection;
	double stusalary;
	cout<<"请输入职工工号:";
	cin>>stucode;
	cout<<endl ;
	cout<<"请输入职工姓名:";
	cin>>stuname;
	cout<<endl ;
	cout<<"请输入职工年龄:";
	cin>>stuage;
	cout<<endl ;
	cout<<"请输入职工性别:";
	cin>>stusex;
	cout<<endl ;
	cout<<"请输入职工邮编:";
	cin>>stumail;
	cout<<endl ;
	cout<<"请输入职工部门:";
	cin>>stusection;
	cout<<endl ;
	cout<<"请输入职工薪资:";
	cin>>stusalary;
	cout<<endl ;
	wor.id=stucode;
	wor.name=stuname;
	wor.age=stuage;
	wor.sex=stusex;
	wor.mail=stumail;
	wor.section=stusection;
	wor.salary=stusalary;
	ver.push_back(wor);
	cout<<"系统正在存储信息,即将返回主界面......"<<endl;
	Sleep(3000) ;
	return;
}
void Plan::st(std::vector<Worker>&ver)   //按职工薪资排名
{
	system("cls") ;
	
	std::sort(ver.begin(), ver.end(), mysalary);     
	list(ver);
		
	return;
}
写项目的时候,自己也遇到过不少问题,比如类的继承和对象访问范围容易犯错,关于vector的用法以及一些需要用的指针的方面,但只要将其搞懂或者上网查一些资料,明白其具体含义及用法后,写起来就会很方面,vector着实很方面,根本不需要你去考虑你想存放数据的大小,很方便,对于这个项目,自己准备再用文件功能将其能保存到本地

更好的实现各个功能!



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值