统计数组中某个数字出现的次数
#include <algorithm>
#include <iostream>
#include <vector>
int main()
{
double data[] = { 1.1, 2.1, 3.1, 4.1, 4.1, 3, 7, 8, 9, 10 };
std::vector<double> v(data, data+10);
double target1 = 3.1;
double target2 = 4.1;
double num_items1 = std::count(v.begin(), v.end(), target1);
double num_items2 = std::count(v.begin(), v.end(), target2);
std::cout << "number: " << target1 << " count: " << num_items1 << '\n';
std::cout << "number: " << target2 << " count: " << num_items2 << '\n';
}
统计数组中满足某一条件的数字出现的次数
#include <algorithm>
#include <iostream>
#include <vector>
int main()
{
int data[] = { 1, 2, 3, 4, 4, 3, 7, 8, 9, 12 };
std::vector<int> v(data, data+10);
int num_items1 = std::count_if(v.begin(), v.end(), [](int i) {return i % 3 == 0;});
std::cout << "number divisible by three: " << num_items1 << '\n';
}