/*********************************************************************************
Copyright(C),Your Company
Author: KimChow
Date: 2021-04-16
Description:c++ vector元素存在判断
count和count_if的用法
Others:
**********************************************************************************/
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct student
{
string name;
int score;
};
bool comp(int num)
{
return num % 2; // 整数取余
}
bool compare(student a)
{
return a.score > 90;
}
int main()
{
vector<int> vecPts;
for (int i = 1; i <= 10; i++)
vecPts.push_back(i);
int target(9);
cout << "目标元素个数为: " << count(vecPts.begin(), vecPts.end(), target) << endl;
cout << "例题:统计1-10奇数的个数为:" << count_if(vecPts.begin(), vecPts.end(), comp) << endl;
cout << "请输入学生人数:";
// 例题:输入一串学生的信息,统计出成绩大于90分的同学个数
int n;
cin >> n;
vector<student> vecStus;
for (int i = 0; i < n; i++)
{
student temp;
cin >> temp.name >> temp.score;
vecStus.push_back(temp);
}
cout << count_if(vecStus.begin(), vecStus.end(), compare) << endl;
return 0;
}
c++ vector元素存在判断
最新推荐文章于 2024-05-30 14:24:47 发布