Skip to content

Commit e248706

Browse files
committed
Drop make_pair and tidy edge cases in answer smash
1 parent 477e3e4 commit e248706

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

Chapter7/Smash.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ std::multimap<std::string, std::string> smashing::load_dictionary(const std::str
3333
//dictionary.insert({ key, value }); or
3434
dictionary.emplace(key, value);
3535
}
36+
else
37+
{
38+
std::cout << "***Invalid line\n" << line << "\nin " << filename << "***\n\n";
39+
}
3640
}
3741
}
3842
else
@@ -55,13 +59,17 @@ std::pair<std::string, int> smashing::find_overlapping_word(std::string word,
5559
if (lb != dictionary.end() &&
5660
stem == lb->first.substr(0, stem.size()))
5761
{
58-
return std::make_pair(lb->first, offset);
62+
return { lb->first, offset };
5963
}
6064
++offset;
6165
}
62-
return std::make_pair("", -1);
66+
return { "", -1 };
6367
}
6468

69+
// Could use
70+
//#include <format>
71+
//#include <string_view>
72+
6573
// Listing 7.6 Simple answer smash game
6674
void smashing::simple_answer_smash(
6775
const std::map<std::string, std::string>& keywords,
@@ -70,10 +78,17 @@ void smashing::simple_answer_smash(
7078
for (const auto& [word, definition] : keywords)
7179
{
7280
auto [second_word, offset] = find_overlapping_word(word, dictionary);
81+
if (offset == -1)
82+
{
83+
std::cout << "Not match for " << word << '\n';
84+
continue;
85+
}
7386
std::string second_definition = dictionary.at(second_word);
7487
std::cout << definition << "\nAND\n" << second_definition << '\n';
7588

7689
std::string answer = word.substr(0, offset) + second_word;
90+
// Or more efficiently,
91+
//std::string answer = std::format("{}{}", std::string_view(word).substr(0, offset), second_word);
7792

7893
std::string response;
7994
std::getline(std::cin, response);

Chapter7/Smash.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ namespace smashing
99
// Listing 7.11 Select a word from a multimap
1010
template <typename T>
1111
std::tuple<std::string, std::string, int> select_overlapping_word_from_dictionary(std::string word,
12-
const std::multimap<std::string, std::string>& dictionary, T select_function)
12+
const std::multimap<std::string, std::string>& dictionary,
13+
T select_function)
1314
{
1415
size_t offset = 1;
1516
while (offset < word.size())
@@ -19,11 +20,9 @@ namespace smashing
1920
auto beyond_stem = stem;
2021
beyond_stem += ('z' + 1);
2122
auto ub = dictionary.upper_bound(beyond_stem);
22-
if (lb != dictionary.end() &&
23-
stem == lb->first.substr(0, stem.size()))
23+
if (lb != dictionary.end() &&
24+
lb != ub)
2425
{
25-
if (lb == ub)
26-
return { lb->first, lb->second, offset };
2726
std::vector<std::pair<std::string, std::string>> dest;
2827
select_function(lb, ub, std::back_inserter(dest));
2928
auto found = dest[0].first;
@@ -32,7 +31,7 @@ namespace smashing
3231
}
3332
++offset;
3433
}
35-
return std::make_tuple("", "", - 1);
34+
return { "", "", -1 };
3635
}
3736

3837
std::pair<std::string, int> find_overlapping_word(std::string word,

Chapter7/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ void hard_coded_game()
181181
{"class", "user defined type with private members by default"},
182182
{"struct", "user defined type with public members by default"},
183183
{"vector", "sequential container supporting dynamic resizing"},
184-
{"template", "used for generic code"},
184+
{"template", "family of classes or functions parameterized by one or more parameters"},
185185
};
186186

187187
const std::map<std::string, std::string> dictionary{

0 commit comments

Comments
 (0)