-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdictionary.cpp
More file actions
39 lines (33 loc) · 1.03 KB
/
Copy pathdictionary.cpp
File metadata and controls
39 lines (33 loc) · 1.03 KB
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
#include "dictionary.h"
#include <fstream>
void Dictionary::addWord(const std::string& word, const std::string& definition) {
entries[word] = definition;
}
void Dictionary::removeWord(const std::string& word) {
entries.erase(word);
}
std::string Dictionary::findWord(const std::string& word) const {
auto it = entries.find(word);
if (it != entries.end()) {
return it->second;
}
return "Word not found.";
}
void Dictionary::listWords() const {
for (const auto& pair : entries) {
std::cout << pair.first << ": " << pair.second << std::endl;
}
}
void Dictionary::loadFromFile(const std::string& filename) {
std::ifstream file(filename);
std::string word, definition;
while (file >> word >> std::ws && std::getline(file, definition)) {
entries[word] = definition;
}
}
void Dictionary::saveToFile(const std::string& filename) const {
std::ofstream file(filename);
for (const auto& pair : entries) {
file << pair.first << " " << pair.second << std::endl;
}
}