Hello.
I am trying to make simple programm to decipher some crypted text (school excersise)
Text is crypted with substitution cipher, I have number of occurences of each letter in the original document.
This code calculates number of occurrences of each character in the crypted text. I need to arrange the results of these code not alphabetical as it does now, but by the number of occurences (from highiest to lowest), assign them to the number of occurrences of the original file and arrange them to the key from crypted file. Can anyone help me please? I am out of ideas to be honest (can provide text documents via email if needed)
Thank you in advance
#include <iostream>
#include <cctype>
#include <sstream>
#include <vector>
#include <map>
#include <algorithm>
usingnamespace std;
int main()
{
map<char,int> frequencies;
stringstream ss( "This is my file: simulated, because I haven't got the original" );
// Read from "file" into a character map
char c;
while( ss >> c ) if ( isalpha( c ) ) frequencies[ toupper( c ) ]++;
// Put in a vector for sorting
vector<pair<char,int>> temp( frequencies.size() );
copy( frequencies.begin(), frequencies.end(), temp.begin() );
// Sort on frequency of occurrence (HIGHEST first)
sort( temp.begin(), temp.end(), []( auto p, auto q ){ return p.second > q.second; } );
// Write out
for ( auto p : temp ) cout << p.first << ": " << p.second << endl;
}