feat: make the reverse map static so it's built once and shared across all MorseDecoder instances
This commit is contained in:
17
main.cpp
17
main.cpp
@@ -42,16 +42,21 @@ string read_input(const string &prompt) {
|
||||
|
||||
class MorseDecoder {
|
||||
private:
|
||||
map<string, char> morse_to_char;
|
||||
|
||||
public:
|
||||
MorseDecoder() {
|
||||
for (const auto &[ch, code] : morse_map) {
|
||||
morse_to_char[code] = ch;
|
||||
static const map<string, char> &get_morse_to_char() {
|
||||
static map<string, char> morse_to_char;
|
||||
static bool initialized = false;
|
||||
if (!initialized) {
|
||||
for (const auto &[ch, code] : morse_map) {
|
||||
morse_to_char[code] = ch;
|
||||
}
|
||||
initialized = true;
|
||||
}
|
||||
return morse_to_char;
|
||||
}
|
||||
|
||||
public:
|
||||
char decode(const string &morse) {
|
||||
const auto &morse_to_char = get_morse_to_char();
|
||||
auto it = morse_to_char.find(morse);
|
||||
return it != morse_to_char.end() ? it->second : '?';
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user