转载请注明出处:http://my.csdn.net/feng1790291543
系统项目:学习STL之后,编写一个学生信息系统,实现简单的增、删、查、改等等功能
类头文件——Student.h
// Student.h: interface for the CStudent class. // ////////////////////////////////////////////////////////////////////// #if !defined(AFX_STUDENT_H__18055DAC_8015_4944_AF62_E78B8EBB233D__INCLUDED_) #define AFX_STUDENT_H__18055DAC_8015_4944_AF62_E78B8EBB233D__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 #include <string> #include <vector> using namespace std; class CStudent { private: int StudentNumber; string StudnetName; vector<CStudent *> student; vector<CStudent> student2; public: CStudent(); virtual ~CStudent(); CStudent(int StudentNumber,string StudnetName); void add_student(); //增加学生信息 void print_student(); //打印学生信息------->(All) void print(); bool operator<(CStudent &stu); //重载学生学号比较大小 void SortStudent(); //给学生信息排序 bool operator()(CStudent &other); //重载函数,模拟回调函数 void FindStudent(); //查找某个学生信息 void DeleteAllStudent(); //删除所有学生信息--->(All) void DeleteOneStudent(); //删除一个学生 }; #endif // !defined(AFX_STUDENT_H__18055DAC_8015_4944_AF62_E78B8EBB233D__INCLUDED_)
Student.cpp源程序函数的实现:
// Student.cpp: implementation of the CStudent class. // ////////////////////////////////////////////////////////////////////// #include "Student.h" #include <iostream> #include <vector> #include <algorithm> #include <functional> using namespace std; ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CStudent::CStudent() { } CStudent::~CStudent() { cout<<"调用析构函数."<<endl; return ; } CStudent::CStudent(int StudentNumber,string StudnetName) { this->StudentNumber=StudentNumber; this->StudnetName=StudnetName; return ; } void CStudent::add_student() //增加学生信息 { cout<<"增加学生."<<endl; int number; string name; cout<<"学生学号:"; cin>>number; cout<<"学生姓名:"; cin>>name; CStudent stu(number,name); student2.push_back(stu); return ; } void CStudent::print() { cout<<"学号:"<<this->StudentNumber<<"姓名:"<<this->StudnetName<<endl; return ; } void CStudent::print_student() //打印学生信息------->(All) { cout<<"打印所有学生的信息."<<endl; for(vector<CStudent>::iterator it=student2.begin();it!=student2.end();it++) //容器从头遍历到尾,end是最后一个元素的后一个 { (*it).print(); } return ; } bool CStudent::operator<(CStudent &stu) //重载学生学号比较大小 { return StudentNumber<stu.StudentNumber; } void CStudent::SortStudent() //给学生信息排序 { sort(student2.begin(),student2.end()); //sort算法,要增加头文件algorithm;默认从小到大排,默认调用operator< ()重载函数 for(vector<CStudent>::iterator it=student2.begin();it!=student2.end();it++) { (*it).print(); } cout<<endl; return ; } bool CStudent::operator()(CStudent &other) //重载函数,模拟回调函数 { bool flag; if(this->StudentNumber==other.StudentNumber||this->StudnetName==other.StudnetName) { flag=true; } else { flag=false; } return flag; } void CStudent::FindStudent() //查找某个学生信息 { int number; string name; cout<<"学生学号:"; cin>>number; cout<<"学生姓名:"; cin>>name; CStudent stu(number,name); vector<CStudent>::iterator result=find_if(student2.begin(),student2.end(),stu); if(result!=student2.end()) { cout<<"Find it:\t"; (*result).print(); } cout<<endl; return ; } void CStudent::DeleteAllStudent() //删除所有学生信息--->(All) { student.clear(); return ; } void CStudent::DeleteOneStudent() //删除一个学生 { int number; string name; cout<<"学生学号:"; cin>>number; cout<<"学生姓名:"; cin>>name; CStudent stu(number,name); vector<CStudent>::iterator result=find_if(student2.begin(),student2.end(),stu); //默认调用operator()括号重载函数 if(result!=student2.end()) { student2.erase(result); //删除位置 } cout<<endl; }
main.cpp
#include "Student.h" #include <iostream> #include <vector> #include <algorithm> #include <functional> using namespace std; int main() { int flag; CStudent s; while(1) { cout<<"1--增加学生信息."<<endl; cout<<"2--打印学生信息."<<endl; cout<<"3--按学号查询学生信息."<<endl; cout<<"4--删除全部学生信息"<<endl; cout<<"5--删除指定学生信息."<<endl; cout<<"6--学生信息排序."<<endl; cout<<"0--退出系统."<<endl; cin>>flag; switch(flag) { case 1: cout<<"增加学生信息."<<endl; s.add_student(); break; case 2: cout<<"打印学生信息."<<endl; s.print_student(); break; case 3: cout<<"按学号查询学生信息:"<<endl; s.FindStudent(); break; case 4: cout<<"删除全部学生信息"<<endl; s.DeleteAllStudent(); break; case 5: cout<<"删除指定学生信息,输入学生学号:"<<endl; s.DeleteOneStudent(); break; case 6: cout<<"学生信息排序."<<endl; s.SortStudent(); break; case 0: cout<<"退出系统."<<endl; return 1; default: break; } } return 0; }运行效果:
C++之基于STL的基本学生信息系统开发(vector/algorithm/functional),布布扣,bubuko.com