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 45
|
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <algorithm>
#include <cctype>
using namespace std;
//======================================================================
template<typename T> int modeCount( const vector<T> &values, vector<T> &mode )
{
map<T,int> freq;
for ( auto e : values ) freq[e]++;
int maxFreq = ( *max_element( freq.begin(), freq.end(), []( auto a, auto b ){ return a.second < b.second; } ) ).second;
mode.clear();
for ( auto p : freq ) if ( p.second == maxFreq ) mode.push_back( p.first );
return maxFreq;
}
//======================================================================
int main()
{
vector<int> days = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
vector<int> imode;
cout << "Modal count is " << modeCount( days, imode ) << " for month(s) with number of days ";
for ( int i : imode ) cout << i << " ";
cout << '\n';
//---------------
string sentence;
cout << "\nEnter a sentence: "; getline( cin, sentence );
vector<char> letters;
for ( char c : sentence ) if ( isalpha( c ) ) letters.push_back( tolower( c ) );
vector<char> cmode;
cout << "Modal letter count is " << modeCount( letters, cmode ) << " for letter(s) ";
for ( char c : cmode ) cout << c << " ";
cout << '\n';
}
|