轉載請注明出處: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#include using namespace std; class CStudent { private: int StudentNumber; string StudnetName; vector student; vector 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: implementation of the CStudent class. // ////////////////////////////////////////////////////////////////////// #include "Student.h" #include#include #include #include using namespace std; ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CStudent::CStudent() { } CStudent::~CStudent() { cout<<"調用析構函數."< StudentNumber=StudentNumber; this->StudnetName=StudnetName; return ; } void CStudent::add_student() //增加學生信息 { cout<<"增加學生."< >number; cout<<"學生姓名:"; cin>>name; CStudent stu(number,name); student2.push_back(stu); return ; } void CStudent::print() { cout<<"學號:"< StudentNumber<<"姓名:"< StudnetName< (All) { cout<<"打印所有學生的信息."< ::iterator it=student2.begin();it!=student2.end();it++) //容器從頭遍歷到尾,end是最後一個元素的後一個 { (*it).print(); } return ; } bool CStudent::operator<(CStudent &stu) //重載學生學號比較大小 { return StudentNumber ::iterator it=student2.begin();it!=student2.end();it++) { (*it).print(); } cout< 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 ::iterator result=find_if(student2.begin(),student2.end(),stu); if(result!=student2.end()) { cout<<"Find it:\t"; (*result).print(); } cout< (All) { student.clear(); return ; } void CStudent::DeleteOneStudent() //刪除一個學生 { int number; string name; cout<<"學生學號:"; cin>>number; cout<<"學生姓名:"; cin>>name; CStudent stu(number,name); vector ::iterator result=find_if(student2.begin(),student2.end(),stu); //默認調用operator()括號重載函數 if(result!=student2.end()) { student2.erase(result); //刪除位置 } cout<
main.cpp#include "Student.h" #include#include #include #include using namespace std; int main() { int flag; CStudent s; while(1) { cout<<"1--增加學生信息."< >flag; switch(flag) { case 1: cout<<"增加學生信息."< 運行效果: