vectordemo10.cpp

来自「《数据结构课程设计案例精编》 附赠光盘源码」· C++ 代码 · 共 42 行

CPP
42
字号
/* 用泛型算法count()和count_if()计数示例 */

#include <vector>
#include <iostream>
#include <algorithm>
#include <string>

using namespace std;

class IsFull 
{
public:
    bool operator() (int & score) 
    {
        return score==100;
    }       
};

int main (void) 
{
  vector <int> Scores;
  
  //构造成绩单
  Scores.push_back(67);  Scores.push_back(89);
  Scores.push_back(100); Scores.push_back(86);
  Scores.push_back(56);  Scores.push_back(100);
  Scores.push_back(99);  Scores.push_back(74);

  int NFullScoresV;     
  NFullScoresV = count (Scores.begin(), Scores.end(), 100);
  cout<<"用count()计数:"<<endl;
  cout << "There were " << NFullScoresV << " fullscores!" << endl;

  int NFullScoresF;     
  NFullScoresF = count_if (Scores.begin(), Scores.end(), IsFull());
  cout<<"用count_if()计数:"<<endl;
  cout << "There were " << NFullScoresF << " fullscores!" << endl;
  
  system("pause");
  return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?