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; }
const float& CPUPercentage::get_percentage() { m_stat_file.open("/proc/stat"); getline(m_stat_file, m_stat_line); m_stat_file.close(); // skip "cpu" m_line_start_pos = m_stat_line.find_first_not_of(" ", 3); m_line_end_pos = m_stat_line.find_first_of(' ', m_line_start_pos); m_line_end_pos = m_stat_line.find_first_of(' ', m_line_end_pos + 1); m_line_end_pos = m_stat_line.find_first_of(' ', m_line_end_pos + 1); m_line_end_pos = m_stat_line.find_first_of(' ', m_line_end_pos + 1); m_iss.str(m_stat_line.substr(m_line_start_pos, m_line_end_pos - m_line_start_pos)); m_iss >> m_next_user >> m_next_nice >> m_next_system >> m_next_idle; m_iss.clear(); m_diff_user = m_next_user - m_current_user; m_diff_system = m_next_system - m_current_system; m_diff_nice = m_next_nice - m_current_nice; m_diff_idle = m_next_idle - m_current_idle; m_percentage = static_cast<float>(m_diff_user + m_diff_system + m_diff_nice)/static_cast<float>(m_diff_user + m_diff_system + m_diff_nice + m_diff_idle)*100.0; m_current_user = m_next_user; m_current_system = m_next_system; m_current_nice = m_next_nice; m_current_idle = m_next_idle; return m_percentage; }
bool FirecrestInput::next_cluster( int *plane, int *ptile, int *px, int* py ) { string line ; if( !getline( intensities_, line ) ) return false ; cur_line_.str( line ) ; cur_line_.clear() ; cycle_ = 0 ; return cur_line_ >> *plane >> *ptile >> *px >> *py ; }
NestedInteger deserialize(istringstream &in) { int number; if(in>>number) return NestedInteger(number); //如果开头是数字,那么一定是单个数的 in.clear(); in.get(); NestedInteger list; while(in.peek() != ']') { list.add(deserialize(in)); if(in.peek()==',') in.get(); } in.get(); return list; }
int main() { #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); freopen("out","w",stdout); #endif scanf("%d\n",&cas); tt=0; while(cas--){ printf("Case #%d: ",++tt); bases.clear(); getline(cin,strtmp); inss.clear(); inss.str(strtmp); while(inss>>a){ bases.push_back(a); } j=2; while(!test(j)){ j++; } printf("%d\n",j); } }
void assign(istringstream &iss, const string &str) { iss.clear(); iss.str(str); }