-
Notifications
You must be signed in to change notification settings - Fork 33
/
16.63.cpp
32 lines (27 loc) · 903 Bytes
/
16.63.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/*
* Exercise 16.63: Define a function template to count the number of
* occurrences of a given value in a vector. Test your program by passing it a
* vector of doubles, a vector of ints, and a vector of strings.
*
* By Faisal Saadatmand
*/
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
template <typename T>
std::size_t v_count(const std::vector<T>& vec, const T &n)
{
// forwarding is not necessary since v_count doesn't change the value of n.
return std::count(vec.cbegin(), vec.cend(), n);
}
int main() {
std::vector<int> ivec{7, 12, 94, 7, 12, 48, 7};
std::cout << v_count(ivec, 7) << '\n';
std::vector<double> dvec{7.3, 12, 94, 7.3, 12, 48, 7.3};
std::cout << v_count(dvec, 7.3) << '\n';
std::vector<std::string>
svec{"three", "one", "ten", "three", "six", "two", "three"};
std::cout << v_count(svec, std::string("three")) << '\n';
return 0;
}