// CPP program to count vector elements that
// match given target value.
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> v{ 10, 30, 30, 10, 30, 30 };
int target = 30;
int res = count(v.begin(), v.end(), target);
cout << "Target: " << target << " Count : " << res << endl;
return 0;
}
count(vect.begin(), vect.end(), 3)
// 1.
for(std::vector<int>::iterator it = vector.begin(); it != vector.end(); ++it)
sum_of_elems += *it;
// 2.
// #include <numeric>
sum_of_elems = std::accumulate(vector.begin(), vector.end(), 0);
// C++11 and higher
// 3.
// #include <numeric>
sum_of_elems = std::accumulate(vector.begin(), vector.end(),
decltype(vector)::value_type(0));
// 4.
for (auto& n : vector)
sum_of_elems += n;
Code Example |
---|
Cpp :: min heap stl |
Cpp :: max c++ |
Cpp :: how to traverse through vector pair |
Cpp :: copy constructor c++ syntax |
Cpp :: c++ polymorphism |
Cpp :: converting char to integer c++ |
Cpp :: c++ set intersection |
Cpp :: how to print items in c++ |
Cpp :: remove something from stringstream |
Cpp :: variadic template in c++ |
Cpp :: bit++ codeforces in c++ |
Cpp :: array 2d to 1d |
Cpp :: assign one vector to another c++ |
Cpp :: STD::ERASE FUNCTION IN C++ |
Cpp :: string append at position c++ |
Cpp :: how to use power in c++ |
Cpp :: new in c++ |
Cpp :: copy constructor for vector c++ |
Cpp :: invert a binary tree |
Cpp :: sort an array in c++ |
Cpp :: cpp compiler online |
Cpp :: substring function in c++ |
Cpp :: fname from FString |
Cpp :: pointer to value of others file cpp |
Cpp :: Code debut C++ |
Cpp :: full pyramid in c++ |
Cpp :: reverse the number codechef solution in c++ |
Cpp :: convert c++ to c online |
Cpp :: pointers mcq sanfoundry |
Cpp :: passing reference to thread c++ |