- Main Page
- Namespaces
- Classes
- Files

# word_count.cpp File Reference

`#include <iomanip>`
`#include <ios>`
`#include <iostream>`
`#include <istream>`
`#include <locale>`
`#include <map>`
`#include <ostream>`
`#include <string>`

Go to the source code of this file.

## Classes
class | function
class | non_letter
class | lowercase
class | sanitizer
## Typedefs
typedef std::map < std::string, int > | count_map
| Map words to counts.
typedef count_map::iterator | count_iter
| Iterate over a `count_map`.
typedef std::string::size_type | str_size
| String size type.
## Functions
void | initialize_streams ()
str_size | get_longest_key (count_map map)
void | print_pair (count_iter iter, str_size longest)
void | print_counts (count_map counts)
int | main ()

## Detailed Description

Definition in file word_count.cpp.

## Typedef Documentation

typedef std::map<std::string, int> count_map

Map words to counts.

Listing 21-2. New main Function that Sets the Global Locale

Definition at line 12 of file word_count.cpp.

## Function Documentation

str_size get_longest_key | ( | count_map | map | ) |

Find the longest key in a map.

Parameters:

  | map | the map to search

Returns:
  the size of the longest key in `map`

Definition at line 31 of file word_count.cpp.

Referenced by print_counts().

void initialize_streams | ( |  | ) |

Initialize the I/O streams by imbuing them with the global locale. Use this function to imbue the streams with the native locale. C++ initially imbues streams with the classic locale.

Definition at line 21 of file word_count.cpp.

Referenced by main().

int main | ( |  | ) |

Main program to count unique words in the standard input.

Definition at line 149 of file word_count.cpp.

References initialize_streams(), and print_counts().

void print_counts | ( | count_map | counts | ) |

Print the results in neat columns.

Parameters:

  | counts | the map of all the counts

Definition at line 57 of file word_count.cpp.

References get_longest_key(), and print_pair().

Referenced by main().

void print_pair | ( | count_iter | iter,
|  | str_size | longest |
| ) |  |  |

Print the word, count, newline. Keep the columns neatly aligned. Rather than the tedious operation of measuring the magnitude of all the counts and then determining the necessary number of columns, just use a sufficiently large value for the counts column.

Parameters:

  | iter | an iterator that points to the word/count pair
  | longest | the size of the longest key; pad all keys to this size

Definition at line 47 of file word_count.cpp.

Referenced by print_counts().