vector<string> HashTables::findMajorityWords_general(istringstream &sin, int k) { unordered_map<string, int> table; string s; int n=0; while (sin >> s) { table[s]++; n++; if (table.size()>=k+1) { for (auto it=table.begin(); it != table.end(); it++){ if (--it->second==0) { // because it must be a item which is just added table.erase(it++); } } } } for (auto &t:table) t.second=0; sin.clear(); sin.seekg(0, ios::beg); while (sin>>s) { auto it = table.find(s); if (it != table.end()) it->second++; } vector<string> ret; for (auto &t: table) { if (t.second>=static_cast<double>(n)/k) ret.emplace_back(t.first); } return ret; }