11.16 Dictionary Lookups: Heap Maps vs Compile-Time Flash Tables
Executive Summary: Building dictionary lookups and evaluating Flash ROM
constexpr lookup tables for embedded systems.
💻 1. Annotated Source Code
#ifndef LANGUAGE_TRANSLATOR_H #define LANGUAGE_TRANSLATOR_H #include <string> #include <map> using namespace std; class LanguageTranslator { public: void addTranslation(const string& englishWord, const string& translatedWord); string getTranslation(const string& englishWord) const; void printAll() const; private: map<string, string> translations; }; #endif
#include "LanguageTranslator.h" #include <iostream> using namespace std; void LanguageTranslator::addTranslation(const string& englishWord, const string& translatedWord) { translations[englishWord] = translatedWord; } string LanguageTranslator::getTranslation(const string& englishWord) const { auto it = translations.find(englishWord); if (it != translations.end()) { return it->second; } else { return "NOT FOUND"; } } void LanguageTranslator::printAll() const { for (const auto& pair : translations) { cout << pair.first << " -> " << pair.second << endl; } }
#include <iostream> #include <string> #include "LanguageTranslator.h" using namespace std; void printMenu(); void handleAdd(LanguageTranslator& translator); void handleLookup(const LanguageTranslator& translator); int main() { LanguageTranslator translator; int choice; printMenu(); cin >> choice; cin.ignore(); while (choice != 0) { if (choice == 1) { handleAdd(translator); } else if (choice == 2) { handleLookup(translator); } else if (choice == 3) { cout << "All translations:" << endl; translator.printAll(); } else { cout << "Invalid choice" << endl; } cout << endl; printMenu(); cin >> choice; cin.ignore(); } return 0; } void printMenu() { cout << "Select an option:" << endl; cout << "1 - Add or update a translation" << endl; cout << "2 - Look up a translation" << endl; cout << "3 - Print all translations" << endl; cout << "0 - Exit" << endl; } void handleAdd(LanguageTranslator& translator) { string englishWord; string translatedWord; cout << "Enter the English word: "; getline(cin, englishWord); cout << "Enter the translation: "; getline(cin, translatedWord); translator.addTranslation(englishWord, translatedWord); } void handleLookup(const LanguageTranslator& translator) { string word; cout << "Enter the English word to translate: "; getline(cin, word); cout << "Translation: " << translator.getTranslation(word) << endl; }
📐 2. Architecture & UML Class Model
<<class>>
LanguageTranslator
Translator Engine
Attributes / Data Members
-dictionary : std::map<std::string, std::string>
Operations / Methods
+addWordPair(source: string, target: string) : void
+translate(source: string) : std::string const
+containsWord(source: string) : bool const
📚 3. Core C++ Concepts Deep-Dive
Multi-Element Map Queries
Managing string-to-string associations and handling missing key lookups.
⚡ 4. Embedded Systems & Hardware Reality
Diagnostic Trouble Codes (DTC)
In automotive firmware, fault code string tables are stored entirely in Flash ROM to save SRAM.
💡 5. Production-Ready Embedded Refactoring
💡 Production-Ready Refactor
constexpr const char* getDtcDescription(uint16_t code) { return (code == 0x0100) ? "Mass Air Flow Fault" : "Unknown"; }
📝 Knowledge Verification Quiz
Test your understanding of the C++ concepts and embedded microcontroller trade-offs covered in this guide. Click any option for instant feedback.
Q1. Where should static lookup tables be stored in embedded systems to conserve SRAM?
Detailed Explanation:
Marking tables
constexpr places them in read-only Flash ROM (.rodata), consuming 0 bytes of SRAM.