文章

9

粉丝

0

获赞

92

访问

1.8k

头像
日志排序 题解: C++ , 存储整串,用 sstream 分解,输出整串
P1227 北京大学机考题
发布于2026年1月21日 20:24
阅读数 594

关键点:

1. istringstream 对 整串分割为   id date time  consumeTime 对应四个参数

2. 耗时(consumeTime) 要存储为 double ,因为长度不固定,不能用字符串直接比较

3. 日期(date) 和时间(time) 可以拼接到一起,用字符串直接比较。

     3.1 因为两个都是定长的,字符串按照字符(ASCII)表逐个比较,效果和分割后,用 int 比较效果一样。

4. sort 排序后输出。

 

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

typedef struct Log{
   string rawLog;
   string id;
   string date;
   string time;
   double consumeTime;
} Log;


int main(){
    string temp;
    vector<Log> logs;
    while(getline(cin, temp) && !temp.empty()){
        Log log;
        log.rawLog = temp; // 原始字符串
        istringstream iss(temp);
        iss >> log.id >> log.date >> log.time >> log.consumeTime; // 解析后字符串

        logs.push_back(log);

...
登录查看完整内容


登录后发布评论

1 条评论
ljh61
2026年1月31日 09:43

牛逼

赞(0)
回复给: