1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
#include <iostream>
#include <map>
class InputHelper {
char const* prompt_ ;
std::ostream& os_ ;
std::istream& is_;
int& n_ ;
public:
InputHelper(std::ostream& o, char const* prompt, std::istream & i, int& n)
: prompt_(prompt), os_(o), is_(i), n_(n) {}
std::istream & operator()()
{
os_ << prompt_ ;
is_ >> n_ ;
return is_ ;
}
};
void print(std::ostream & os, const std::map<int,unsigned> m)
{
std::map<int, unsigned>::const_iterator m_iter = m.begin() ;
while ( m_iter != m.end() )
{
os << (*m_iter).first << " occurred " << (*m_iter).second << " times.\n" ;
++m_iter ;
}
}
int main ()
{
std::map<int, unsigned> item_map ;
int num ;
InputHelper get_input(std::cout, "Please enter a number (0 to quit): ", std::cin, num) ;
while (get_input() && num != 0)
item_map[num]++ ;
print( std::cout, item_map ) ;
}
|