#include <iostream>
#include <fstream>
#include <vector>
#include <string>
std::vector<std::string> load_names_from_file(const std::string& filename) {
std::vector<std::string> names;
std::ifstream infile(filename);
std::string name;
while (std::getline(infile, name)) {
names.push_back(name);
}
return names;
}
void save_names_to_file(const std::string& filename, const std::vector<std::string>& names) {
std::ofstream outfile(filename);
for (const auto& name : names) {
outfile << name << std::endl;
}
}
int main() {
const std::string filename = "student_names.txt";
std::vector<std::string> names = load_names_from_file(filename);
char choice;
std::string name_to_add;
do {
std::cout << "查询学生姓名 (q退出): ";
std::getline(std::cin, name_to_add);
if (name_to_add != "q") {
auto it = std::find(names.begin(), names.end(), name_to_add);
if (it != names.end()) {
std::cout << "找到了姓名: " << *it << std::endl;
} else {
std::cout << "未找到姓名: " << name_to_add << std::endl;
}
}
} while (name_to_add != "q");
// 保存数据到文件
save_names_to_file(filename, names);
return 0;
}
error:
[Error] no matching function for call to 'find(std::vector<std::__cxx11::basic_string >::iterator, std::vector<std::__cxx11::basic_string >::iterator, std::string&)'