STL Map Container
The STL Map container
main.cpp
// Project 11-01
// Name: Dictionary of Terms#include <iostream>
#include <string>
#include <map>
#include "Dictionary.h"
void printMainMenu();
#include <iostream>
using namespace std;
int main()
{
cout << "/---------------------------------------------------\\" << endl;
cout << "| A Dictionary of Terms |" << endl;
cout << "| ================================= |" << endl;
cout << "| Course: The Complete C++ Developer Course |" << endl;
cout << "| Instructor: John P. Baugh, Ph.D. |" << endl;
cout << "| Student: Patrick Kox |" << endl;
cout << "\\---------------------------------------------------/" << endl << endl;
Dictionary dictionary;
bool quit = false;
int selection = 1;
while (!quit) {
cout << endl;
printMainMenu();
cin >> selection;
cin.get();
if (selection == 1) {
string word;
string definition;
cout << "Enter the word: ";
getline(cin, word);
cout << "Enter the definition for " << word << ": ";
getline(cin, definition);
dictionary.addDefinition(word, definition);
}
if (selection == 2) {
string word;
cout << "For which word do you want the definition? ";
getline(cin, word );
cout << dictionary.getDefinition(word) << endl << endl;
}
if (selection == 3) {
dictionary.printAll();
cout << endl << endl;
}
if (selection == 0) {
quit = true;
}
}
return 0;
}
void printMainMenu()
{
cout << "Type your selection" << endl;
cout << "\t1 - Add a new word and definition." << endl;
cout << "\t2 - Get the definition for a word." << endl;
cout << "\t3 - print all definitions." << endl;
cout << "\t0 - Exit the program." << endl;
}
Dictionary.h
#ifndef DICTIONARY_H
#define DICTIONARY_H
#include <map>
#include <string>
using namespace std;
class Dictionary
{
public:
void addDefinition(string word, string definition);
string getDefinition(string word);
void printAll();
private:
map<string, string> dictionary;
};
#endif
Dictionary.cpp
#include "Dictionary.h"
#include <iostream>
void Dictionary::addDefinition(string word, string definition)
{
if (dictionary.find(word) == dictionary.end()) {
dictionary[word]=definition;
// auto i = dictionary.find(word);
// i->second = definition;
}
else {
auto i = dictionary.find(word);
i->second = definition;
}
}
string Dictionary::getDefinition(string word)
{
cout << "Looking up " << word << "..." << endl<< endl;
if (dictionary.find(word) == dictionary.end()) {
return "NOT FOUND!";
}
else {
auto i = dictionary.find(word);
return i->second;
}
}
void Dictionary::printAll()
{
for (pair<string, string> element : dictionary) {
cout << element.first << " = " << element.second << endl;
}
}