-
Notifications
You must be signed in to change notification settings - Fork 0
/
dictionary.cpp
37 lines (33 loc) · 995 Bytes
/
dictionary.cpp
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
#include "dictionary.h"
#include <iostream>
Dictionary::Dictionary(std::fstream& fs){
std::string input;
while(fs >> input){
if(input == "##"){
fs.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
else{
char c[kMAXLINELENGTH];
fs.getline(c, kMAXLINELENGTH, '\n');
std::stringstream ss{c};
dict.push_back(DictEntry{input, ss});
index.insert(std::pair<std::string, int>{input, dict.size()-1});
}
}
}
class notInDictionaryException{
public:
notInDictionaryException(const std::string& str){
std::cout << str << '\n';
}
};
int Dictionary::lookup(std::string& query){
std::unordered_map<std::string,int>::const_iterator it = index.find(query);
if(it != index.end()){
return it->second;
};
throw notInDictionaryException(query);
}
DictEntry& Dictionary::at(int num){
return dict[num];
}