C 语言

关注公众号 jb51net

关闭
首页 > 软件编程 > C 语言 > C++日期字符串排序处理方法

在C++中正确处理日期字符串排序的方法

作者:weixin_pk138132

在C++编程中,排序是一个核心任务,std::sort(来自 <algorithm>)是一个极其强大的神奇黑盒,可以为你排序 vector,本文给大家介绍了在C++中正确处理日期字符串排序的方法,需要的朋友可以参考下

在C++编程中,排序是一个核心任务。std::sort(来自 <algorithm>)是一个极其强大的“神奇黑盒”,可以为你排序 vector

但是,如果你有一堆“日期”字符串呢?
vector<string> dates = {"10-12-2023", "05-01-2024"};

如果你“天真地”调用 std::sort
std::sort(dates.begin(), dates.end());

一个简单的比喻:“字典” vs “日历”

解决方案:
我们不能直接排序字符串。我们必须:

  1. “教会” C++ 如何“阅读”日期。最好的方法是创建一个“自定义盒子”(struct),把日期拆分成“年”、“月”、“日”三个整数。
  2. “教会” std::sort 我们的“日历规则”。我们必须提供一个“自定义比较器”(Comparator)函数,告诉 std::sort:“先比较年份,如果年份相同,再比较月份…

在本教程中,你将学会:

前置知识说明 (100% 自洽):

第一部分:“自定义盒子”——Date结构体

首先,我们“设计”一个“蓝图”,告诉C++一个 Date 对象长什么样。

#include <iostream>
#include <vector>
#include <algorithm> // 包含 std::sort
#include <string>
using namespace std;

// “蓝图”:一个自定义的 Date 盒子
// 它把日期“拆分”成三个 *可比较* 的整数
struct Date {
    int day;
    int month;
    int year;
};

现在,我们可以创建 Date 对象的 vector,而不是 stringvector
vector<Date> calendar = { {10, 12, 2023}, {5, 1, 2024}, {15, 12, 2023} };

第二部分:“日历规则”——自定义比较器

std::sort 的“默认规则”是 operator< (小于号)。我们现在要提供一个的“规则手册”(一个函数),来代替 <

比较器 (Comparator) 的“黄金法则”:
std::sort 需要一个函数 bool compare(A, B)

compareDates.cpp (“日历规则”函数)

// “规则手册”:告诉 std::sort 如何比较两个 Date 对象
bool compareDates(const Date& a, const Date& b) {
    
    // 规则 1:先比较“年”
    if (a.year < b.year) {
        return true;  // a 的年份小,a 应该排在前面
    }
    if (a.year > b.year) {
        return false; // a 的年份大,a 应该排在后面
    }
    
    // “行内预警”:如果程序运行到这里,说明 a.year == b.year
    
    // 规则 2:如果年份相同,再比较“月”
    if (a.month < b.month) {
        return true;  // a 的月份小,a 应该排在前面
    }
    if (a.month > b.month) {
        return false; // a 的月份大,a 应该排在后面
    }
    
    // “行内预警”:如果程序运行到这里,说明年份和月份都相同
    
    // 规则 3:如果月份相同,最后比较“日”
    return (a.day < b.day); // 如果 a.day 小,返回 true
}

第三部分:“实战演练”——结合sort与“规则”

现在我们把 std::sort 和我们的“规则手册” compareDates 结合起来。

date_sort.cpp (完整代码)

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;

// --- 1. “蓝图” ---
struct Date {
    int day;
    int month;
    int year;
};

// --- 2. “规则手册” ---
bool compareDates(const Date& a, const Date& b) {
    if (a.year < b.year) return true;
    if (a.year > b.year) return false;
    
    // 年份相同
    if (a.month < b.month) return true;
    if (a.month > b.month) return false;
    
    // 月份也相同
    return (a.day < b.day);
}

// 辅助函数:打印日期
void printDates(const string& title, const vector<Date>& dates) {
    cout << title << endl;
    for (const auto& d : dates) {
        // (为了美观,我们补 0)
        printf("  %02d-%02d-%04d\n", d.day, d.month, d.year);
    }
}


int main() {
    // 3. 创建“自定义盒子”列表
    vector<Date> calendar = {
        {10, 12, 2023}, // 10-Dec-2023
        {5,  1,  2024}, // 05-Jan-2024
        {15, 12, 2023}, // 15-Dec-2023
        {2,  2,  2023}  // 02-Feb-2023
    };
    
    printDates("--- 原始顺序 ---", calendar);

    // 4. “按下按钮”并“递上纸条” (规则手册)
    std::sort(calendar.begin(), calendar.end(), compareDates);

    printDates("\n--- 日历排序后 ---", calendar);
    
    return 0;
}

“手把手”终端模拟:

PS C:\MyCode> g++ date_sort.cpp -o date_sort.exe -std=c++11
PS C:\MyCode> .\date_sort.exe
--- 原始顺序 ---
  10-12-2023
  05-01-2024
  15-12-2023
  02-02-2023

--- 日历排序后 ---
  02-02-2023
  10-12-2023
  15-12-2023
  05-01-2024

顿悟时刻: 排序成功std::sort 正确地理解了我们的“日历规则”,将 2023 年的日期排在了 2024 年的前面。

第四部分:“X光透 视”——亲眼目睹“规则”被调用

我们无法(也不需要)用调试器“步入” (F11) std::sort 内部,因为它是一个高度优化的、编译好的“黑盒”。
但是,我们可以“步入”它调用我们的“自定义规则” (compareDates)!

“X光”实战(基于date_sort.cpp)

设置断点:

启动“子弹时间”(F5):

开启“X光”(观察“裁判”工作):

继续执行 (F5)。

第二次“冻结” (在 compareDates 内部):

(继续按 F5,sort 会调用你的“裁判”很多次…)

动手试试!(终极挑战:你的“学生排名器”)

现在,你来当一次“教务处”老师。

任务:

  1. 设计一个 Student 结构体
    • string name;
    • int score; (分数)
    • int age;
  2. 创建一个 vector<Student> 并填入一些数据。
  3. 编写一个“规则手册”bool compareStudents(const Student& a, const Student& b)
  4. (关键) 排序规则是:
      1. 优先按分数 (score) 降序(分数高的排前面)。
      1. 如果分数相同,则按年龄 (age) 升序(年龄小的排前面)。
  5. 调用 std::sort,传入你的“规则手册”。
  6. 打印排序后的学生列表。

student_sort.cpp (你的 TODO):

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;

// --- TODO 1: Student 结构体 ---
struct Student {
    string name;
    int score;
    int age;
};

// --- TODO 3 & 4: 编写 *新* 的“规则” ---
// (按分数降序, 同分按年龄升序)
// bool compareStudents(const Student& a, const Student& b) {
//     // 1. 先比较分数
//     if (a.score > b.score) return true;  // 分数高,排前面
//     if (a.score < b.score) return false; // 分数低,排后面
    
//     // 2. 如果分数相同 (a.score == b.score)
//     //    再比较年龄 (升序)
//     // return a.age < b.age; // 年龄小,排前面
// }

int main() {
    // --- TODO 2: 创建数据 ---
    vector<Student> students = {
        {"Alice", 90, 20},
        {"Bob", 85, 22},
        {"Charlie", 90, 19}, // <-- 和 Alice 同分
        {"David", 70, 21}
    };
    
    cout << "--- 原始列表 ---" << endl;
    // (在此处添加 for 循环打印)

    // --- TODO 5: 调用 sort ---
    // std::sort(students.begin(), students.end(), ...);

    cout << "\n--- 排序后 ---" << endl;
    // --- TODO 6: 打印结果 ---
    // (在此处添加 for 循环打印)
    // 预测:Charlie (90, 19) 应该在 Alice (90, 20) 之前
    
    return 0;
}

这个挑战让你实践了如何编写一个“多级”比较规则,这是 std::sort 在真实项目中极其常见的用法!

以上就是在C++中正确处理日期字符串排序的方法的详细内容,更多关于C++日期字符串排序处理方法的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
阅读全文