11.13 std::map for Dynamic Configuration & Lookup Tables
Executive Summary: Using associative mappings for key-value pair storage, with focus on lookup mechanics and embedded ROM alternative tables.
💻 1. Annotated Source Code
#include <iostream> #include <string> #include <map> using namespace std; int main() { map<string, string> contacts; contacts["John Baugh"] = "313-555-5555"; contacts["Bob Robinson"] = "734-555-5050"; contacts["Sally Snorkle"] = "810-555-8888"; for (auto element : contacts) { cout << element.first << " = " << element.second << endl; } return 0; }
📐 2. Architecture & UML Class Model
<<struct>>
Contact
Contact Record
Attributes / Data Members
+name : std::string
+phone : std::string
+email : std::string
Operations / Methods
+printContact() : void const
<<class>>
ContactBook
Address Book
Attributes / Data Members
-contacts : std::map<std::string, Contact>
Operations / Methods
+addContact(c: const Contact&) : void
+findContact(name: string) : Contact*
+removeContact(name: string) : bool
+displayAll() : void const
🔗 Architectural Relationships & Hierarchy
ContactBook
◆──
maps name to contact
◆──
Contact
📚 3. Core C++ Concepts Deep-Dive
Associative Access
Maps allow lookup by arbitrary key types using balanced search trees.
⚡ 4. Embedded Systems & Hardware Reality
Flash ROM Lookups
In firmware, key-value mappings are often stored in Flash ROM as constant arrays to conserve SRAM.
💡 5. Production-Ready Embedded Refactoring
💡 Production-Ready Refactor
struct KeyValue { const char* key; const char* val; }; static constexpr KeyValue CONFIG_LUT[] = { {"BAUD", "115200"}, {"NODE_ID", "0x42"} };
📝 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. What is the key lookup complexity in std::map?
Detailed Explanation:
std::map is a Red-Black Tree providing guaranteed $O(\log N)$ search time.