1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
#include <vector>
#include <unordered_map>
#include <iostream>
#include <iomanip>
int main() {
const std::vector<int> nums = { 1, 2, 3, 4, 5, 5, 5, 5, 5, 4, 3, 3, 1, 4, 1, 4, 5, 4, 4, 5 };
std::unordered_map< int, int > numbers_seen;
for( int v : nums ) ++numbers_seen[v] ; // update frequencies in map
std::cout << "number frequency\n"
"------ ---------\n" ;
for( const auto& pair : numbers_seen ) // print out key, mapped value pairs
std::cout << std::setw(4) << pair.first << std::setw(10) << pair.second << '\n' ;
}
|