题解 | #简单错误记录#

简单错误记录

https://www.nowcoder.com/practice/2baa6aba39214d6ea91a2e03dff3fbeb

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

using namespace std;
string GetFileName(string& str);
typedef struct 
{
    string filename;
    unsigned int line;
    unsigned int cnt;
}Record;
void InputRecord(void);

int main() {
    InputRecord();
}
// 64 位输出请用 printf("%lld")

//特点:
//1. 提取文件名
//2. 需要去除相同的错误记录
//相同的定义:文件名+行号 都相同,则为相同的错误记录
//3. 结果展示最后8个。

//获取文件名:
//找到最后一个/,然后从这个位置开始得到文件名
//如果文件名长度大于16,则截取最后的16个
string GetFileName(string &str)
{
    //strrchr()
    auto idx = str.rfind('\\');
    string f;
    if(idx == string::npos) return f;
    
    string filestr(str, idx+1);
    auto len = filestr.size();
    if(len > 16)
    {
        filestr = filestr.substr(len-16);
    }
    return filestr;
}

void InputRecord(void)
{
    vector<Record> rcdTbl;  
    string tempin;

    while(getline(cin,tempin))
    {
        stringstream ss(tempin);
        string pathfile;
        string t_file;
        unsigned int t_line;
        ss >> pathfile;
        t_file = GetFileName(pathfile);
        ss >> t_line;
        auto iter=rcdTbl.begin();
        for(;iter!=rcdTbl.end(); iter++)
        {
            if((iter->filename == t_file) &&(iter->line == t_line))
            {
                iter->cnt++;
                break;
            }
        }
        if(iter == rcdTbl.end())
        {
            Record newRec;
            newRec.cnt = 1;
            newRec.filename = t_file;
            newRec.line = t_line;
            rcdTbl.push_back(newRec);
        }
    }
    auto iter=rcdTbl.begin();
    if(rcdTbl.size()>8)
    {
        iter += rcdTbl.size()-8;
    }
    for(; iter!=rcdTbl.end(); iter++)
    {
        cout << iter->filename << " "<<iter->line <<" " << \
        iter->cnt << endl;
    }
}

全部评论

相关推荐

学一下吧现在太菜了:和简历没关系,你是清华的他就要了。多投投就行了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务