简单错误记录
简单错误记录
http://www.nowcoder.com/questionTerminal/2baa6aba39214d6ea91a2e03dff3fbeb
惭愧,使用了双重循环。
#include <iostream> #include <string> #include <vector> #include <map> #include <unordered_map> using namespace std; string getfilename(string& str) { auto pos = str.find_last_of("\\"); str = str.substr(pos + 1); pos = str.find(" "); if (pos > 16) { str = str.substr(pos - 16); } return str; } int main() { string str; unsigned lineNo; //string rst[8]; unsigned cnt = 0; pair<string, unsigned> par; vector<pair<string, unsigned> > rst; //unorder_map<string, unsigned> rst; while(getline(cin, str)) { str = getfilename(str); bool flag = false; for(auto it = rst.begin(); it != rst.end(); ++it) { if(it->first == str) { it->second++; flag = true; break; } } if(!flag) { rst.push_back({str, 1}); } } cnt = 0; if(rst.size() > 8) { cnt = rst.size() - 8; } for(auto it = rst.begin() + cnt; it != rst.end(); ++it) { cout << it->first << " " << it->second << endl; } return 0; }