Skip to content

Commit

Permalink
Chapter 3: Add Exercise 3-5
Browse files Browse the repository at this point in the history
  • Loading branch information
asankov committed May 9, 2020
1 parent bea9778 commit d15f071
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
5 changes: 5 additions & 0 deletions chapter-3/3.6-c++/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@

### Section 3.6 C++
**Summary:** Let's rewrite these stuff in [C++](markov.cpp)!

#### Exercise 3-5
The great strength of the STL is the ease with which one can experiment with different data structures.
Modify the C++ version of Markov to use various structures to represent the prefix, suffix list, and state table.
How does performance change for the different structures?
6 changes: 4 additions & 2 deletions chapter-3/3.6-c++/markov.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
#include <vector>

typedef std::deque<std::string> Prefix;
std::map<Prefix, std::vector<std::string> > statetab;
typedef std::vector<std::string> Suffixes;
typedef std::map<Prefix, Suffixes> State;
State statetab;

enum
{
Expand Down Expand Up @@ -59,7 +61,7 @@ void generate(int nwords)
srand(time(NULL));
for (int i = 0; i < nwords; i++)
{
std::vector<std::string> &suf = statetab[prefix];
Suffixes &suf = statetab[prefix];
const std::string &w = suf[rand() % suf.size()];
if (w == NONWORD)
break;
Expand Down

0 comments on commit d15f071

Please sign in to comment.