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

STL

开发平台:

C/C++

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