home *** CD-ROM | disk | FTP | other *** search
- Using a dictionary to count the number of occurrences of the elements in
- a sequence of strings, terminated by string ``stop''.
- \smallskip
- \#include $<$LEDA/dictionary.h$>$
- \smallskip
- {\bf declare2}(dictionary, string, int)
- \smallskip
- \cleartabs
- \+main()\cr
- \+$\{$\ \ &\cr
- \+ &dictionary(string, int) $D$;\cr
- \smallskip
- \+ &string $s$;\cr
- \+ &dic\_item $it$;\cr
- \smallskip
- \+ &{\bf while} &( (cin $>> s$) \&\& ($s$ != ``stop'') )\cr
- \+ &\ \ &$\{$ &$it = D$.lookup($s$);\cr
- \+ & & &if ($it==nil$) $D$.insert($s,1$);\cr
- \+ & & &else $D$.change\_inf($it$,$D$.info($it$)+1);\cr
- \+ & &\ $\}$\cr
- \smallskip
- \+ &{\bf forall\_dic\_items}($it,D$)
- cout $<< D$.info($it$) $<<$ `` " $<<$ $D$.key($it$) $<<$ ``$\backslash$n";\cr
- \smallskip
- \+\ $\}$\cr
-
-