基本思想
1.增加
輸入要增加的資訊,直接用vector的push_back()
void Student::Add() {
cout << "please input the add information" << endl << endl;
Student s;//之前定義的學生類
s.input();
vec.push_back(s);
print();//一個輸出函數
}
2.查找
查找可以分很多種,比如,按學号查找,姓名查找等等,但核心思想都是一樣,這裡隻介紹用姓名查找。首先,輸入要查找的學生姓名,用疊代器iterator進行周遊一遍,然後 進行判斷是否有相同,若有相同,輸出該學生資訊,跳出循環,若循環走完都沒有找到直接輸出“查無此人”。
cout << "please input the find name" << endl << endl;
string na;
cin >> na;
int flag = 0;//用于标記是否找到
for (it = vec.begin(); it != vec.end(); it++) {
if (na == it->getname()) {
it->output();
flag = 1;
break;
}
}
if(!flag)
cout << "查無此人" << endl;
3.修改
建立在查找的基礎上,隻不過是在周遊判斷之後,若相同的話,在進行一次資訊的輸入,對原來的資訊進行覆寫即可。若不同直接輸出“本無此人”。
cout << "please input the change name" << endl << endl;
string na;
cin >> na;
int flag = 0;
for (it = vec.begin(); it != vec.end(); it++) {
if (na == it->getname()) {
cout << "please input the change information" << endl << endl;
it->input();//直接覆寫
flag = 1;
break;
}
}
if (!flag)
cout << "查無此人" << endl;
4.删除
也是建立在查找的基礎上,找到之後直接用erase(删除位址)進行删除,要注意erase的參數是要删除的資訊的位址,可以用指針表示。
cout << "please input the delete name" << endl << endl;
string na;
cin >> na;
int flag = 0;
for (it = vec.begin(); it != vec.end(); it++) {
if (na == it->getname()) {
vec.erase(it);
flag = 1;
break;
}
}
if (!flag)
cout << "查無此人" << endl;
最後,給個全代碼吧,但希望大家最好還是自己想之後進行打出代碼,不能一味的抄襲别人的代碼,但是我也很懂代碼打不出來時候的難受,是以給個全代碼僅限參考。(一起加油!!!)
// 資料結構1.cpp : 定義控制台應用程式的入口點。
//
#include "stdafx.h"
#include<iostream>
#include<vector>
#include<string>
using namespace std;
class Student {
string name;
float usual, finnal, aver;
public:
vector<Student> vec;
vector<Student>::iterator it;
Student(string na="0", float u=0, float f=0) { name = na; usual = u; finnal = f; }
string getname() { return name; }
void input();
void Init(Student &s);//錄入
float compute(float u, float f) { aver = (u + f)/2; return aver; }
void output();
void print();
void Add();
void Delete();
void Find();
void Change();
};
void Student::input() {
cin >> name >> usual >> finnal;
}
void Student::Init(Student &s) {
cout << "Please input name,usual,finnal" << endl << endl;
s.input();
vec.push_back(s);
}
void Student::output() {
compute(usual, finnal);
cout << name << " " << usual << " " << finnal <<" "<<aver<< endl;
}
void Student::print() {
for (it = vec.begin(); it != vec.end(); it++)
//(*it).output();
it->output();
}
void Student::Add() {
cout << "please input the add information" << endl << endl;
Student s;
s.input();
vec.push_back(s);
print();
}
void Student::Delete() {
cout << "please input the delete name" << endl << endl;
string na;
cin >> na;
int flag = 0;
for (it = vec.begin(); it != vec.end(); it++) {
if (na == it->getname()) {
vec.erase(it);
flag = 1;
break;
}
}
if (!flag)
cout << "查無此人" << endl;
print();
}
void Student::Find() {
cout << "please input the find name" << endl << endl;
string na;
cin >> na;
int flag = 0;
for (it = vec.begin(); it != vec.end(); it++) {
if (na == it->getname()) {
it->output();
flag = 1;
break;
}
}
if(!flag)
cout << "查無此人" << endl;
}
void Student::Change() {
cout << "please input the change name" << endl << endl;
string na;
cin >> na;
int flag = 0;
for (it = vec.begin(); it != vec.end(); it++) {
if (na == it->getname()) {
cout << "please input the change information" << endl << endl;
it->input();//直接覆寫
flag = 1;
break;
}
}
if (!flag)
cout << "查無此人" << endl;
print();
}
int main()
{
Student s1, s2, s3;
Student s;
s.Init(s1);
s.Init(s2);
s.Init(s3);
s.print();
s.Add();
s.Change();
s.Delete();
s.Find();
system("pause");
return 0;
}