39void split (
const std::string& text,
const std::string& separators,
40 std::list<std::string,std::allocator<std::string> > &words)
43 const std::string::size_type
n = text.length ();
44 std::string::size_type
start = text.find_first_not_of (separators);
47 std::string::size_type stop = text.find_first_of (separators,
start);
48 if (stop >
n) stop =
n;
49 words.push_back (text.substr (
start, stop-
start));
50 start = text.find_first_not_of (separators, stop+1);
57 typedef std::multimap<std::string,int,
58 std::less<std::string>,
59 std::allocator<std::pair<
const std::string,
60 int> > > wordDictType;
62 void addWord (std::string,
int);
73 wordDictType::iterator low = wordMap.lower_bound (word);
74 wordDictType::iterator high = wordMap.upper_bound (word);
77 for ( ; low != high; ++low)
78 if ( (*low).second ==
line)
82 wordMap.insert (wordDictType::value_type (word,
line));
88 for (std::size_t
i = 0;
i <
s.size ();
i++)
89 if ((std::isupper)(
s [
i], std::locale ()))
90 s [
i] = (std::tolower)(
s [
i], std::locale ());
98 for (
int i = 1; std::getline (
in,
line);
i++) {
100 std::list<std::string, std::allocator<std::string> > words;
102 std::list<std::string, std::allocator<std::string> >::iterator wptr;
103 for (wptr = words.begin (); wptr != words.end (); ++wptr)
111 std::string lastword (
"");
112 wordDictType::iterator pairPtr;
113 wordDictType::iterator stop = wordMap.end ();
115 for (pairPtr = wordMap.begin (); pairPtr != stop; ++pairPtr)
117 if (lastword == (*pairPtr).first)
118 out <<
' ' << (*pairPtr).second;
121 lastword = (*pairPtr).first;
122 std::cout <<
'\n' << lastword <<
": " << (*pairPtr).second;
130 std::cout <<
"Concordance sample program, from Chapter 7\n"
131 <<
"Enter text, then end-of-file:\n";
138 std::cout <<
"End of concordance sample program\n";
void addWord(std::string, int)
void printConcordance(std::ostream &)
void readText(std::istream &)
void allLower(std::string &s)
void split(const std::string &text, const std::string &separators, std::list< std::string, std::allocator< std::string > > &words)