So we were given the task of writing a compression alg for a .txt of text/numbers (presumably through huffman coding since our professor was very vague)
I have all the lines as keys in a map with frequencies as their values. I'm a little sketchy on how to proceed from here since maps are organized in order by key not value Should I be using a different data structure (not a map) or would it be easy enough to just find the 2 smallest min values every time I wanted to add to the tree? Code below, any help would be awesome!
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;
int main()
{
vector <string> words;
map <string, int> store;
ifstream infile("file.txt");
string text;
while (getline(infile, text))
{
istringstream iss(text);
string input;
if (!(iss >> input))
break;
words.push_back(input);
}
int freq = 0;
while (!words.empty())
{
string check = words[0];
if(check == "") //make sure not reading a blank
{
words.erase(remove(words.begin(), words.end(), "")); //remove all blanks
continue; //top of loop
}
check = words[0];
freq = count(words.begin(), words.end(), check);//calculate frequency
store.insert(pair<string, int>(check, freq)); //store words and frequency in map
words.erase(remove(words.begin(), words.end(), check)); //erase that value entirely from the vector
}
map<string, int>::iterator i;
for(i = store.begin(); i != store.end(); ++i)
{
cout << "store[" << i ->first << "] = " << i->second << '\n';
}
return 0;
}