技术点
一:iterator (迭代器)
是一个广义的指针,亦可以是一个可对其执行类似指针的操作; 如: 要为vector的double类型规范声明一个iterator,可以这样做
vector<double>::iterator pd;//pd an iterator假设scores是一个对象
vector<double>scores; pd=scores.begin();//hava pd point to the first element *pd=22.3;//dereference pd and asssign valueto the first element ++pd;//make pd point to the next element正想你说看到的,iterator的行为就像指针。
push_back();是一个方便的方法,他将元素添加到矢量的末尾,并且负责管理内存,增加矢量的长度,使得能够容纳新的成员。
迭代器(iterator)是一种对象,它能够用来遍历标准模板库容器中的部分或全部元素,每个迭代器对象代表容器中的确定的地址。迭代器修改了常规指针的接口,所谓迭代器是一种概念上的抽象:那些行为上像迭代器的东西都可以叫做迭代器。然而迭代器有很多不同的能力,它可以把抽象容器和通用算法有机的统一起来。 迭代器提供一些基本操作符:*、++、==、!=、=。这些操作和C/C++“操作array元素”时的指针接口一致。不同之处在于,迭代器是个所谓的复杂的指针,具有遍历复杂数据结构的能力。其下层运行机制取决于其所遍历的数据结构。因此,每一种容器型都必须提供自己的迭代器。事实上每一种容器都将其迭代器以嵌套的方式定义于内部。因此各种迭代器的接口相同,型号却不同。这直接导出了泛型程序设计的概念:所有操作行为都使用相同接口,虽然它们的型别不同。 功能 迭代器使开发人员能够在类或结构中支持foreach迭代,而不必整个实现IEnumerable或者IEnumerator接口。只需提供一个迭代器,即可遍历类中的数据结构。当编译器检测到迭代器时,将自动生成IEnumerable接口或者IEnumerator接口的Current,MoveNext和Dispose方法http://baike.baidu.com/link?url=bGvXrhXbGZtLFNJH9mwYXc7VEHQ1UqHPwZkq198-hALxUh9MeSnFI-cCff5LSbMmjyhnx8DsXZhAi7xMjEiLbJpg8TC7krXHNgZBeVP6MWjd70qxDCi2Y6HB10HCv_CJ
有时又称游标(cursor)是程序设计的软件设计模式,可在容器(container,例如链表或阵列)上遍访的接口,设计人员无需关心容器的内容,能够用来遍历容器的对象。
二: vector (动态容器),功能类似数组,内含多种属性,加上iterator,搭配使用。
如:
vector<employee> itr=stu.begin();//creat an opint to stu.begin可以创建vector对象,将一个vector对象的值赋给另一个对象,使用【】来访问对象成员。方便创建动态分配的数组,之外还有所有的STL容器都提供了一些基本的方法,其中包括size()//返回容器中元素的个数 swap()//交换两个容器的内容 begin()//返回一个指向容器中第一个元素的iretator end()//返回一个超过文件尾的iretator 初始化:vector
三: 遍历,一种查询机制
是指沿着某条搜索路线,依次对树中每个结点均做一次且仅做一次访问。访问结点所做的操作依赖于具体的应用问题。 遍历是二叉树上最重要的运算之一,是二叉树上进行其它运算之基础。当然遍历的概念也适合于多元素集合的情况,如数组。
小型工资系统代码
#include <iostream> #include <stdio.h> #include <algorithm> #include <string> #include <vector> #include <fstream> using namespace std; class employee { private: float salary; int day, year; string id,name, tele, address, office, sex, rate, password; vector<employee> stu; static int now_year; public: employee() {} employee(string id, string name, string sex, string tele, string address, string office, float salary, int year, int day) { this->id = id; this->salary = salary; this->sex = sex; this->name = name; this->tele = tele; this->address = address; this->office = office; this->year = year; this->day = day; } void Addemployee(); void readerfile(); void inputid(); void Delectemployee(); void Updaemployee(); void reseacheremployeebyname(); void reseacheremployeebyoffice(); void reseacheremployeebyid(); void SaveFile(); void Updacode(string &username); vector<string> split(string str, string separator) { vector<string> pas; int cutAt; while ((cutAt = str.find_first_of(separator)) != str.npos) { if (cutAt > 0) { pas.push_back(str.substr(0, cutAt)); } str = str.substr(cutAt + 1); } if (str.length() > 0) { pas.push_back(str); } return pas; } }; int employee::now_year = 2017; int main() { employee employ; employ.inputid(); return 0; } void employee::SaveFile() { fstream file; file.open("file3.txt", ios::out);//写入文件用cout vector<employee>::iterator itr = stu.begin(); while (itr != stu.end()) { file << (*itr).id << "\n"; file << (*itr).name << "\n"; file << (*itr).sex << "\n"; file << (*itr).tele << "\n"; file << (*itr).address << "\n"; file << (*itr).office << "\n"; file << (*itr).salary << "\n"; file << (*itr).year << "\n"; file << (*itr).now_year << "\n"; file << (*itr).day << "\n"; itr++; } file.close(); } void employee::Addemployee() { int day; float salary; string username, name, tele, address, office, sex; int year; cout << "请输入职工编号\n"; cin >> username; vector<employee>::iterator itr = stu.begin(); bool same =true; //定义bool型变量用于判断添加的用户是否存在。如果存在,不允许添加。 while (itr != stu.end()) { if((*itr).id==username){ same = false; break; } itr++; } if(same==1){ if (username.size() == 8&&(username.find('A') != string::npos||username.find('B') != string::npos||username.find('C') != string::npos||username.find('D') != string::npos||username.find('E') != string::npos||username.find('F') != string::npos)) { cout << "请输入职工名字\n"; cin >> name; cout << "请输入职工性别\n"; cin >> sex; cout << "请输入职工电话号码\n"; cin >> tele; tele.size(); if (tele.size() == 11) { cout << "请输入职工地址\n"; cin >> address; cout << "请输入职工科室\n"; cin >> office; cout << "请输入职工薪水\n"; cin >> salary; cout << "请输入职工入职年份\n"; cin >> year; if (year<2017 || year == 2017) { cout << "请你输入职工本月工作天数\n"; cin >> day; employee T(id, name, sex, tele, address, office, salary, day, year); stu.push_back(T); ofstream ffile; //添加成功时会把用户名和密码写到user文件中去 ffile.open("user.txt",ios::app); if(!ffile) { cout << "Error opening file "; exit (1); } ffile<<username<<"\t"<<username<<endl; ffile.close(); SaveFile(); //保存新增员工的信息 cout << "你已成功添加职工,现有" << stu.size() << "个职工" << endl; } else cout << "您输入的信息不符合标准,添加失败!请重新输入!\n"; } else cout << "您输入的信息不符合标准,添加失败!请重新输入!\n"; } else cout << "您输入的信息不符合标准,添加失败!请重新输入!\n"; } else cout<<"你输入的用户名已存在,请重新输入!\n"; } void employee::Delectemployee() { cout << "请输入要删除的职工号"<<endl; string id; cin >> id; fstream imfile; fstream isfile; //打开文件“user” isfile.open("user.txt",ios::in|ios::out); imfile.open("usertemp.txt",ios::out); //建立一个新文件"usertemp" string buff; while(getline(isfile, buff)) { //每次读取一行存到buff中 if(id == split(buff,"\t").at(0)){ //split(buff,"\t").at(0) 按照\t分隔字符串buff,分割后,第一部分at(0)是用户名,第二部分at(1)是密码 } else { imfile<<buff<<endl; } } isfile.close(); imfile.close(); if(remove("user.txt")==-1)perror("remove"); if(rename("usertemp.txt","user.txt")==-1)perror("rename"); vector<employee>::iterator p = stu.begin(); bool delect =false; //定义bool型变量,用于判断是否存在该用户 while (p != stu.end()) { if ((*p).id == id) { stu.erase(p); delect=true; break; } p++; } SaveFile(); if(delect) { cout << "你已成功删除该职工" << endl; } else cout<<"删除失败!该员工不存在!"<<endl; } void employee::Updaemployee() { int day; float salary; string id, name, tele, address, office, sex, year; cout << "输入你要修改的职工号\n"; cin >> id; vector<employee>::iterator p = stu.begin(); while (p != stu.end()) { if ((*p).id == id) { cout << "请输入修改职工的姓名\n"; cin >> name; cout << "请输入修改职工的性别\n"; cin >> sex; cout << "请输入修改职工的电话号码\n"; cin >> tele; cout << "请输入修改职工的地址\n"; cin >> address; cout << "请输入修改职工的科室\n"; cin >> office; cout << "请输入修改职工的薪水\n"; cin >> salary; cout << "请输入修改职工的入职年份\n"; cin >> year; (*p).id = id; (*p).name = name; (*p).sex = sex; (*p).tele = tele; (*p).address = address; (*p).office = office; (*p).salary = salary; break; } p++; } SaveFile(); } void employee::reseacheremployeebyname() { cout << "请输入你要查询的人的名字\n"; string name; cin >> name; vector<employee>::iterator p = stu.begin(); while (p != stu.end()) { if ((*p).name == name) { cout << "职工的编号:\n" << (*p).id << endl; cout << "职工的姓名:\n" << (*p).name << endl; cout << "职工的性别:\n" << (*p).sex << endl; cout << "职工的电话号码:\n" << (*p).tele << endl; cout << "职工的地址:\n" << (*p).address << endl; cout << "职工的科室:\n" << (*p).office << endl; cout << "职工的薪水:\n" << (*p).salary << endl; cout << "职工的入职年份:\n" << (*p).year << endl; cout << "职工本月在职天数:\n" << (*p).day << endl; break; } p++; } } void employee::reseacheremployeebyoffice() { cout << "请输入你要查询的人的科室\n"; string office; cin >> office; vector<employee>::iterator p = stu.begin(); while (p != stu.end()) { if ((*p).office == office) { cout << "职工的编号:\n" << (*p).id << endl; cout << "职工的姓名:\n" << (*p).name << endl; cout << "职工的性别:\n" << (*p).sex << endl; cout << "职工的电话号码:\n" << (*p).tele << endl; cout << "职工的地址:\n" << (*p).address << endl; cout << "职工的科室:\n" << (*p).office << endl; cout << "职工的薪水:\n" << (*p).salary << endl; cout << "职工的入职年份:\n" << (*p).year << endl; cout << "职工本月在职天数: \n" << (*p).day << endl; } p++; } } void employee::reseacheremployeebyid() { cout << "请输入你要查询的人的ID\n"; string id; cin >> id; vector<employee>::iterator p = stu.begin(); while (p != stu.end()) { if ((*p).id == id) { cout << "职工的编号:\n" << (*p).id << endl; cout << "职工的姓名:\n" << (*p).name << endl; cout << "职工的性别:\n" << (*p).sex << endl; cout << "职工的电话号码:\n" << (*p).tele << endl; cout << "职工的地址:\n" << (*p).address << endl; cout << "职工的科室:\n" << (*p).office << endl; cout << "职工的薪水:\n" << (*p).salary << endl; cout << "职工的入职年份:\n" << (*p).year << endl; cout << "职工本月在职天数: \n" << (*p).day << endl; } p++; } } void employee::readerfile() { fstream infile("file3.txt", ios::in); vector<employee>::iterator inf = stu.begin();//这个数组是动态数组没有定义,就是说里面是空的!没有任何东西!指向首地址就是指向末尾地址 //employee of();//程序声明对象时,将自动调用构造函数!!!! //int id ,string name,string sex,string tele,string address,string office,int salary int d; float sala; string n, i, s, t, a, o, now; int y; while (infile.peek() != std::ifstream::traits_type::eof())//判断是否到达文件尾部,以防止文件读取出现错误 { infile >> i >> n >> s >> t >> a >> o >> sala >> y >> now >> d;//找到啦!!!!就是这里 employee of(i, n, s, t, a, o, sala, y, d); stu.push_back(of);//只能添加数组!!不能直接加数字 } infile.close(); } void employee::inputid() { readerfile(); string username,password; cout << "请输入您的ID:\n"; cin >> username; int input; if (username.size() == 8) { cout<<"请您输入密码:"<<endl; cin >> password; //string leader_code = "A123456"; //密码先等到全部改为是私有的时候在修改!!!! //ID.size(); fstream infile; //打开文件“user” infile.open("user.txt"); if(!infile) { cout << "Error opening file "; exit (1); } string buff; bool loginSuccess = false; while(getline(infile, buff)){ //每次读取一行存到buff中 if(username == split(buff,"\t").at(0)){ //split(buff,"\t").at(0) 按照\t分隔字符串buff,分割后,第一部分at(0)是用户名,第二部分at(1)是密码 if(password == split(buff,"\t").at(1)){ cout << "login success" << endl; loginSuccess = true; if (username.find('A') != string::npos)//string::npos,是指没有找到! { while (true) { cout << "========================职工工资管理系统=========================" << '\n' << "1.按姓名查询职工" << '\n' << "2.按科室查询职工" << '\n' << "3.按ID查询职工" << '\n' << "4.修改密码" << '\n' << "0.退出职工工资管理系统" << '\n' << "========================职工工资管理系统=========================" << '\n'; cin >> input; switch (input) { case 1:reseacheremployeebyname(); break; case 2:reseacheremployeebyoffice(); break; case 3:reseacheremployeebyid(); break; case 4:infile.close();Updacode(username);infile.open("user.txt"); break; //case 4:change_code(); break; case 0:exit(1); break; } } } else if (username.find("B") != string::npos)//人事部门 { while (true) { cout << "========================职工工资管理系统=========================" << '\n' << "1.添加职工" << '\n' << "2.删除职工" << '\n' << "3.修改职工" << '\n' << "4.按姓名查询职工" << '\n' << "5.按科室查询职工" << '\n' << "6.按ID查询职工" << '\n' << "7.修改密码" << '\n' << "0.退出职工工资管理系统" << '\n' << "========================职工工资管理系统=========================" << '\n'; cin >> input; switch (input) { case 1:Addemployee(); break; case 2: infile.close();Delectemployee();infile.open("user.txt"); break; case 3: Updaemployee(); break; case 4:reseacheremployeebyname(); break; case 5:reseacheremployeebyoffice(); break; case 6:reseacheremployeebyid(); break; case 7:infile.close();Updacode(username);infile.open("user.txt");break; case 0:exit(1); break; } } } else if (username.find("C") != string::npos)//只能查询销售里面的人员的 { while (true) { cout << "========================职工工资管理系统=========================" << '\n' << "1.按ID查询职工" << '\n' << "2.修改密码" << '\n' << "0.退出职工工资管理系统" << '\n' << "========================职工工资管理系统=========================" << '\n'; cin>>input; switch (input) { case 1:reseacheremployeebyid(); break; case 2:infile.close();Updacode(username);infile.open("user.txt"); break; case 0:exit(1); break; } } } else if (username.find("D") != string::npos)//办公室 { while (true) { cout << "========================职工工资管理系统=========================" << '\n' << "1.按ID查询职工" << '\n' << "2.修改密码" << '\n' << "0.退出职工工资管理系统" << '\n' << "========================职工工资管理系统=========================" << '\n'; cin>>input; switch (input) { case 1:reseacheremployeebyid(); break; case 2:infile.close();Updacode(username);infile.open("user.txt"); break; case 0:exit(1); break; } } } else if (username.find("E") != string::npos) { while (true) { cout << "========================职工工资管理系统=========================" << '\n' << "1.按ID查询职工" << '\n' << "2.修改密码" << '\n' << "0.退出职工工资管理系统" << '\n' << "========================职工工资管理系统=========================" << '\n'; cin>>input; switch (input) { case 1:reseacheremployeebyid(); break; case 2:infile.close();Updacode(username);infile.open("user.txt"); break; case 0:exit(1); break; } } } else if (username.find("F") != string::npos) { while (true) { cout << "========================职工工资管理系统=========================" << '\n' << "1.按ID查询职工" << '\n' << "2.修改密码" << '\n' << "0.退出职工工资管理系统" << '\n' << "========================职工工资管理系统=========================" << '\n'; cin>>input; switch (input) { case 1:reseacheremployeebyid(); break; case 2:infile.close();Updacode(username);infile.open("user.txt"); break; case 0:exit(1); break; } } } else cout << "您输入的ID有误!1" << endl; } else { cout<<"您输入的密码不正确!"<<endl; loginSuccess = true; } break; } } if(!loginSuccess) cout << "user does not exist" << endl; infile.close(); } //if (ID[0] > '@ '&& ID[0]<'G')//判断首字母在A到G之间, else cout << "您输入的ID有误!3\n"; } /*c++暂时提供不了文件的修改功能,只能将文件重新写一次 1.获取新密码,和这个用户的用户名 2.创建一个新文件user.temp 3.按行读取user文件 4.按照\t分隔这一行,取出用户名 5.比对用户名是否是需要改密码的那个人 6.如果不是,则将这一行写入user.temp 7.如果是,则将用户名+\t+新密码写入user.temp(加号不用写进去) 8.读取下一行,重复4-8 9.删除user文件,将user.temp重命名为user */ void employee::Updacode(string &username) { cout<<"请输入新密码:"<<endl; cin>>password; cout<<"再次输入:"<<endl; string password1; cin>>password1; if(password1==password) { fstream imfile; fstream isfile; //打开文件“user” isfile.open("user.txt",ios::in|ios::out); imfile.open("usertemp.txt",ios::out); //建立一个新文件"usertemp" string buff; while(getline(isfile, buff)){ //每次读取一行存到buff中 if(username == split(buff,"\t").at(0)){ //split(buff,"\t").at(0) 按照\t分隔字符串buff,分割后,第一部分at(0)是用户名,第二部分at(1)是密码 imfile<<username<<"\t"<<password<<endl; } else { imfile<<buff<<endl; } } isfile.close(); imfile.close(); if(remove("user.txt")==-1)perror("remove"); if(rename("usertemp.txt","user.txt")==-1)perror("rename"); cout<<"修改成功!"<<endl; } else cout<<"两次输入的密码不正确!"<<endl; }