学校机房写代码,求改
  • 板块灌水区
  • 楼主ZhangXuKun
  • 当前回复1
  • 已保存回复1
  • 发布时间2024/10/12 11:31
  • 上次更新2024/10/12 16:49:04
查看原帖
学校机房写代码,求改
1325697
ZhangXuKun楼主2024/10/12 11:31
#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&)'

2024/10/12 11:31
加载中...