? ? ? ? 這是我在以前面試中遇到的一個問題,?面試官說:你能現場實現一個學生管理系統嗎,實現對學生的增刪查改這4個功能
? ? ? ? 當時寫了半天沒寫出來.....,所以我在這里記錄一下
10分鐘實現學生管理系統并實現 增刪查改 功能
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>// 學生類
struct Student {int id;// 學號std::string name;// 名字double score;// 分數// 打印學生信息void print() const {std::cout << "ID: " << id << ", Name: " << name << ", Score: " << score << std::endl;}
};// 管理員類
class Admin {
public:// 添加學生void addStudent(int id, const std::string& name, double score) {students.push_back({ id, name, score });// 會調用默認的構造函數生成匿名學生std::cout << "添加學生成功!" << std::endl;}// 刪除學生void deleteStudent(int id) {auto it = std::remove_if(students.begin(), students.end(), [id](const Student& s) {return s.id == id;});if (it != students.end()) {students.erase(it,students.end());std::cout << "刪除學生成功!" << std::endl;}else {std::cout << "學生不存在!" << std::endl;}}// 查找學生void findStudent(int id) const {auto it = std::find_if(students.begin(), students.end(), [id](const Student& s) {return s.id == id;});if (it != students.end()) {it->print();// 調用學生的打印}else {std::cout << "學生不存在!" << std::endl;}}// 更新學生void updateStudent(int id, const std::string& newName, double newScore) {auto it = std::find_if(students.begin(), students.end(), [id](const Student& s) {return s.id == id;});if (it != students.end()) {it->name = newName;it->score = newScore;std::cout << "更新學生成功!" << std::endl;}else {std::cout << "學生不存在!" << std::endl;}}// 列出所有學生信息void listAllStudents() const {for (const auto& student : students) {student.print();}}
private:std::vector<Student> students;//用數組存儲學生
};int main() {Admin manager;std::string name;int id = 0;double score = 0.0;int choice = 0;while (true) {std::cout << "1. 添加學生" << std::endl;std::cout << "2. 刪除學生" << std::endl;std::cout << "3. 查找學生" << std::endl;std::cout << "4. 更新學生" << std::endl;std::cout << "5. 列出所有學生信息" << std::endl;std::cout << "6. 退出" << std::endl;std::cout << "Enter your choice: ";std::cin >> choice;switch (choice) {case 1:std::cout << "請輸入學號 姓名 分數";std::cin >> id >> name >> score;manager.addStudent(id, name, score);break;case 2:std::cout << "請輸入要刪除學生的學號";std::cin >> id;manager.deleteStudent(id);break;case 3:std::cout << "請輸入要查找學生的學號";std::cin >> id;manager.findStudent(id);break;case 4:std::cout << "請輸入要更新學生的學號,姓名,分數";std::cin >> id >> name >> score;manager.updateStudent(id, name, score);break;case 5:manager.listAllStudents();break;case 6:return 0;// 表正常退出default:std::cout << "輸入不合法,請重新輸入" << std::endl;}}return 0;
}
remove_if
???????remove_if是 C++ <algorithm>
頭文件中的一個算法,?將不符合條件的元素移動到前面,并返回新的結束迭代器,需要配合容器的 erase()
方法來真正刪除元素(再刪除一段訪問)
#include <algorithm> // std::remove_if
#include <vector> // std::vector
#include <iostream> // std::cout
using namespace std;int main() {std::vector<int> vec = {1,3, 4, 5, 6};auto end_begin = std::remove_if(vec.begin(), vec.end(), [](int x){return x % 2 == 0;//移除所有偶數});vec.erase(end_begin, vec.end());// 刪除一段訪問for (int n : vec) {std::cout << n << " "; // 輸出:1 3 5}cout << endl;
}
find_if
????????find_if
是 C++ <algorithm>
頭文件中的一個算法,用于查找容器中第一個符合指定條件的元素,并返回指向該元素的迭代器。如果找不到,則返回 end()
迭代器?
#include <iostream>
#include <vector>
#include <algorithm> // std::find_ifint main() {std::vector<int> vec = {1, 3, 5, 4, 6, 7};auto it = std::find_if(vec.begin(), vec.end(), [](int x){return x % 2 == 0;});if (it != vec.end()) {std::cout << "找到第一個偶數:" << *it << std::endl; // 輸出:找到第一個偶數:4} else {std::cout << "未找到偶數" << std::endl;}
}
?