CHAPTER6-13.cpp
上传用户:fjc899
上传日期:2007-07-03
资源大小:187k
文件大小:1k
源码类别:

STL

开发平台:

C/C++

  1. //文件名:CHAPTER6-13.cpp
  2. #pragma warning(disable:4786)
  3. #include <iostream.h>
  4. #include <string>
  5. #include <vector>
  6. #include <algorithm>
  7. using namespace std;
  8. class IsAToothbrush 
  9. {
  10. public:
  11. IsAToothbrush(string& InToothbrushCode) : ToothbrushCode(InToothbrushCode) {}
  12. bool operator() (string& SalesRecord) 
  13. { return SalesRecord.substr(0,4)==ToothbrushCode; }       
  14. private:
  15.   string ToothbrushCode;        
  16. };
  17. void main (void) 
  18. {
  19.   vector<string> SalesRecords;
  20.   SalesRecords.push_back("0001 Soap");
  21.   SalesRecords.push_back("0002 Shampoo");
  22.   SalesRecords.push_back("0003 Toothbrush");
  23.   SalesRecords.push_back("0004 Toothpaste");
  24.   SalesRecords.push_back("0003 Toothbrush");
  25.   string VariableToothbrushCode("0003");
  26.   int NumberOfToothbrushes(0);  
  27.   NumberOfToothbrushes = count_if (SalesRecords.begin(), SalesRecords.end(), 
  28. IsAToothbrush(VariableToothbrushCode));
  29.   cout << "There were  " << NumberOfToothbrushes << " toothbrushes matching code "<< "0003" << " sold" << endl;
  30. }