setexclusion.cpp
上传用户:qdkongtiao
上传日期:2022-06-29
资源大小:356k
文件大小:3k
源码类别:

书籍源码

开发平台:

Visual C++

  1. /*
  2.  * This file contains code from "C++ Primer, Fourth Edition", by Stanley B.
  3.  * Lippman, Jose Lajoie, and Barbara E. Moo, and is covered under the
  4.  * copyright and warranty notices given in that book:
  5.  * 
  6.  * "Copyright (c) 2005 by Objectwrite, Inc., Jose Lajoie, and Barbara E. Moo."
  7.  * 
  8.  * 
  9.  * "The authors and publisher have taken care in the preparation of this book,
  10.  * but make no expressed or implied warranty of any kind and assume no
  11.  * responsibility for errors or omissions. No liability is assumed for
  12.  * incidental or consequential damages in connection with or arising out of the
  13.  * use of the information or programs contained herein."
  14.  * 
  15.  * Permission is granted for this code to be used for educational purposes in
  16.  * association with the book, given proper citation if and when posted or
  17.  * reproduced.Any commercial use of this code requires the explicit written
  18.  * permission of the publisher, Addison-Wesley Professional, a division of
  19.  * Pearson Education, Inc. Send your request for permission, stating clearly
  20.  * what code you would like to use, and in what specific way, to the following
  21.  * address: 
  22.  * 
  23.  *  Pearson Education, Inc.
  24.  *  Rights and Contracts Department
  25.  *  75 Arlington Street, Suite 300
  26.  *  Boston, MA 02216
  27.  *  Fax: (617) 848-7047
  28. */ 
  29. #include "assoc_preamble.h"
  30. #include "seq_preamble.h"
  31. #include <fstream>
  32. using std::ifstream;
  33. void restricted_wc(ifstream &remove_file,
  34.                    map<string, int> &word_count)
  35. {
  36.     set<string> excluded;   // set to hold words we'll ignore
  37.     string remove_word;
  38.     while (remove_file >> remove_word)
  39.         excluded.insert(remove_word);
  40.     // print contents of the word exclusion set
  41.     for (set<string>::iterator set_it = excluded.begin();
  42.                                set_it != excluded.end();
  43.                                ++set_it)
  44.          cout << *set_it << endl;
  45.     // read input and keep a count for words that aren't in the exclusion set
  46.     string word;
  47.     while (cin >> word)
  48.        // increment counter only if the word is not in excluded
  49.        if (!excluded.count(word))
  50.             ++word_count[word];
  51. }
  52. // call with the name of the exclusion set file
  53. int main(int argc, char **argv) 
  54. {
  55.     ifstream remove_file;
  56.     open_file(remove_file, argv[1]);  // file of words to ignore
  57.     map<string, int> word_count;
  58.     restricted_wc(remove_file, word_count);
  59.     // get iterator positioned on the first element
  60.     map<string, int>::iterator map_it = word_count.begin();
  61.     // for each element in the map
  62.     while (map_it != word_count.end()) {
  63.         // print the element key, value pairs
  64.         cout << map_it->first << " occurs "
  65.              << map_it->second << " times" << endl;
  66.         ++map_it;  // increment iterator to denote the next element
  67.     }
  68.     return 0;
  69. }