tailieunhanh - Thinking in C plus plus (P24)

Tham khảo tài liệu 'thinking in c plus plus (p24)', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | A map of vectors include map include vector include string include iostream include algorithm include ctime using namespace std typedef map string vector string Thesaurus typedef pair string vector string TEntry typedef Thesaurus iterator TIter ostream operator ostream os const TEntry t os copy ostream_iterator string os return os A generator for thesaurus test entries class ThesaurusGen static const string letters static int count public int maxSize return ThesaurusGen srand time 0 TEntry operator TEntry result if count maxSize count 0 letters count int entries rand 5 2 for int i 0 i entries i int choice rand maxSize char cbuf 2 0 cbuf 0 letters choice cbuf return result int ThesaurusGen count 0 Chapter 15 Multiple Inheritance 251 const string ThesaurusGen letters ABCDEFGHIJKL MNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz int main Thesaurus thesaurus Fill with 10 entries generate_n inserter thesaurus 10 ThesaurusGen Print everything copy ostream_iterator TEntry cout n Ask for a word to look up while true cout Select a word 0 to quit for TIter it it it cout it .first cout endl string reply cin reply if 0 0 return 0 Quit if reply continue Not in list try again vector string v thesaurus reply copy ostream_iterator string cout cout endl A Thesaurus maps a string the word to a vector string the synonyms . A TEntry is a single entry in a Thesaurus. By creating an ostream operator for a TEntry a single entry from the Thesaurus can easily be printed and the whole Thesaurus can easily be printed with copy . The ThesaurusGen creates words which are just single letters and synonyms for those words which are just other randomly-chosen single letters to be used as thesaurus entries. It randomly chooses the number of synonym entries to make but there must be at least two.