-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrequency_table.cpp
More file actions
29 lines (26 loc) · 851 Bytes
/
Copy pathfrequency_table.cpp
File metadata and controls
29 lines (26 loc) · 851 Bytes
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
#include "frequency_table.h"
#include <fstream>
using namespace std;
frequency_table::frequency_table(const std::string &file_name) { // constructs object. Read file, store freq. throw runtime_error if file isn't found or can't be open
for(int c = 0; c < 128; c++){
map.insert({char(c), 0});
}
ifstream inFile(file_name);
char c;
if(inFile.good()){
while(inFile.get(c)){
map.at(c)++;
}
}else{
throw runtime_error("File not found");
}
}
frequency_table::~frequency_table() { // destructs object, frees dynamic memory
map.erase(map.begin(), map.end());
}
int frequency_table::get_frequency(char c) const { // returns frequency of character c in the file
return map.at(c);
}
int frequency_table::getSize() const{
return map.size();
}