You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++患者信息管理项目:结构体/类选型及排序问题咨询

C++患者记录项目:排序解决与结构体/类选择建议

一、按字母顺序排序的解决方案

情况1:单文本行排序(如示例所示)

如果是要把所有输入的文本行按字符串字典序直接排序,直接用std::sort处理存储行的容器即可,C++字符串默认的比较规则就是字典序。

代码实现:

#include <iostream>
#include <vector>
#include <algorithm>
#include <fstream>
#include <string>

int main() {
    std::vector<std::string> recordLines;
    std::string inputLine;

    // 读取所有输入行(可替换为从文件读取)
    while (std::getline(std::cin, inputLine)) {
        recordLines.push_back(inputLine);
    }

    // 执行字典序排序
    std::sort(recordLines.begin(), recordLines.end());

    // 输出结果或写入文件
    std::ofstream outputFile("sorted_records.txt");
    for (const auto& line : recordLines) {
        std::cout << line << "\n";
        outputFile << line << "\n";
    }
    outputFile.close();

    return 0;
}

情况2:按患者整体记录排序(更符合实际业务)

如果是要将完整的患者记录(每个患者的ID、姓名等信息为一组)按某个字段(如姓名)排序,需要先将输入解析为结构化数据,再自定义排序规则:

  1. 先定义数据结构存储患者信息;
  2. 解析输入行,将同一患者的信息整合为一个对象;
  3. 用std::sort结合自定义比较函数排序。

示例代码(用结构体):

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <sstream>

// 定义患者数据结构
struct Patient {
    std::string id;
    std::string name;
    std::string surname;
    std::string email;
    std::string diagnosis;
};

// 自定义比较函数:按姓名(name+surname)字典序排序
bool compareByName(const Patient& a, const Patient& b) {
    std::string fullNameA = a.name + " " + a.surname;
    std::string fullNameB = b.name + " " + b.surname;
    return fullNameA < fullNameB;
}

int main() {
    std::vector<Patient> patients;
    Patient currentPatient;
    std::string line;

    // 解析输入行(假设输入按患者分组,每组结束后有空行或特定标识)
    while (std::getline(std::cin, line)) {
        if (line.empty()) {
            // 空行表示当前患者信息输入完成,加入列表
            patients.push_back(currentPatient);
            // 重置当前患者对象
            currentPatient = Patient{};
            continue;
        }

        std::istringstream iss(line);
        std::string key, value;
        std::getline(iss, key, ':');
        std::getline(iss, value);

        // 匹配字段并赋值
        if (key == "patient id" || key == "patientid") {
            currentPatient.id = value;
        } else if (key == "name") {
            currentPatient.name = value;
        } else if (key == "surname") {
            currentPatient.surname = value;
        } else if (key == "email") {
            currentPatient.email = value;
        } else if (key == "diagnosis") {
            currentPatient.diagnosis = value;
        }
    }
    // 加入最后一个患者
    if (!currentPatient.id.empty()) {
        patients.push_back(currentPatient);
    }

    // 按姓名排序
    std::sort(patients.begin(), patients.end(), compareByName);

    // 输出排序后的患者信息
    for (const auto& p : patients) {
        std::cout << "patient id:" << p.id << "\n";
        std::cout << "name:" << p.name << "\n";
        std::cout << "surname:" << p.surname << "\n";
        if (!p.email.empty()) std::cout << "email:" << p.email << "\n";
        if (!p.diagnosis.empty()) std::cout << "diagnosis:" << p.diagnosis << "\n";
        std::cout << "\n";
    }

    return 0;
}

二、结构体还是类的选择建议

C++中结构体和类的核心差异仅在于默认访问权限:结构体成员默认public,类成员默认private,其他语法特性完全一致。

选结构体的场景

如果你的项目目前仅需要纯数据载体,不需要封装复杂的业务逻辑(比如只是存储患者的各项信息,没有验证、格式化等行为),用结构体更合适:

  • 代码更简洁,无需手动设置public权限;
  • 直接访问成员变量更高效,适合快速开发数据存储模块。

选类的场景

如果后续需要扩展功能,比如:

  • 添加邮箱格式验证、诊断信息格式化等成员函数;
  • 隐藏内部数据细节,仅通过get/set接口访问成员变量;
  • 实现继承、多态等面向对象特性;
    这时类是更好的选择,因为它天然支持封装,能更好地维护代码的可扩展性和安全性。

总结

初期开发阶段用结构体快速实现数据存储;当需要添加业务逻辑或封装需求时,再重构为类(或直接一开始就用类,预留扩展空间),两种方式的转换成本极低。

内容的提问来源于stack exchange,提问作者Kabo Mphanyane

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 06:35:29