为错误记录创建一个类Record,用来记录每一条错误记录,包含名称、数目统计、代码行数信息,使用vector记录错误信息,输出最后8条即可。
#include <iostream> #include <vector> using namespace std; class Record { public: Record(string addr, int num) { this->name = addr.substr(addr.rfind('\\') + 1); if (this->name.length() > 16) { this->name = this->name.substr(this->name.size() - 16); } this->num = num; count = 1; } string getName() const { return name; } int getNum() const { return num; } int getCount() const { return count; } void add() { count++; } private: string name; int num; int count; }; bool isHave(vector<Record> & records, string addr, int num) { addr = addr.substr(addr.rfind('\\') + 1); if (addr.length() > 16) { addr = addr.substr(addr.size() - 16); } for (Record & record : records) { if (record.getName() == addr && record.getNum() == num) { record.add(); return true; } } return false; } int main() { string s; int num; vector<Record> records; while (cin >> s >> num) { if (!isHave(records, s, num)) { records.push_back(Record(s, num)); } } int start = (int)records.size() - 8 > 0 ? (int)records.size() - 8 : 0; for (int i = start; i < records.size(); i++) { cout << records.at(i).getName() << ' ' << records.at(i).getNum() << ' ' << records.at(i).getCount() << endl; } return 0; } // 64 位输出请用 printf("%lld")