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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
#include <cctype>
#include <iostream>
#include <fstream>
#include <functional>
#include <string>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;
// comparison function for sort:
bool by_frequency(const pair<string, int>& a, const pair<string, int>& b)
{
return a.second < b.second;
}
// sanitize a string. Keep alphanumeric characters, make it all the same case.
string sanitize(string txt)
{
txt.erase(remove_if(txt.begin(), txt.end(), not1(isalnum)), txt.end());
transform(txt.begin(), txt.end(), txt.begin(), tolower);
return txt;
}
int main()
{
// Öppnar filen
std::ifstream book("hitchhikersguide.txt"); // prefer to define objects near first use.
// Kollar att filen öppnades korrekt.
if (!book.is_open())
cout << "Filen öppnades inte korrekt." << endl;
// Lägger in värdena i book till tempbook som sen kollar ifall ordet
// finns i map:en words. Om det inte hittas lägger vi till värdet 1.
// Hittas ordet lägger plussar vi på 1 till ordets värde.
string tempbook; // prefer to define objects near first use
map<string, int> words;
while (book >> tempbook)
{
tempbook = sanitize(tempbook);
//if (words.find(tempbook) == words.end())
// words[tempbook] = 1;
//else
// words[tempbook]++;
// value_types are default initialized (set to 0 in the case of int,) which means
// the following will b equivalent to the code commented out above:
++words[tempbook];
}
// Lägger in orden och deras respektive värden som par i en vector.
//for (map<string, int>::iterator it = words.begin(); it != words.end(); ++it)
//{
// wordsVec.push_back(*it);
//}
vector<pair<string,int>> wordsVec(words.begin(), words.end()); // prefer to define objects near first use.
sort(wordsVec.begin(), wordsVec.end(), by_frequency);
// Skriver ut vår vector.
for (vector<pair<string,int>>::iterator i = wordsVec.begin(); i != wordsVec.end(); ++i)
{
cout << i->first << " finns i texten " << i->second << " gånger." << endl;
}
}
|