count 计数等于 value
的元素。
int main()
{
std::vector<int> v{ 1, 2, 3, 4, 4, 3, 7, 8, 9, 10 };
// 确定 std::vector 中有多少个整数匹配目标值。
int target1 = 3;
int target2 = 5;
int num_items1 = std::count(v.begin(), v.end(), target1);
int 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';
// 用 lambda 表达式计量能被 3 整除的元素数。
int num_items3 = std::count_if(v.begin(), v.end(), [](int i) {return i % 3 == 0; });
std::cout << "number divisible by three: " << num_items3 << '\n';
}
标签:std,count,begin,cout,int,算法,num From: https://www.cnblogs.com/asd12310/p/16743136.html