b) Write a template version of the function count to determine the number of times a specific value occurs in a vector of any base type and test it by declaring two or more vectors with different base types and determining how many times a specific value occurs in these two vectors.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
std::vector <double>values;
//...
//Erase all elements and read the elements from the input standard
values.clear();
double x;
int n;
cout<<"Enter the number you want to show its occurance"<<'\n';
cin>>n;
while(std::cin>>x)
{
values.push_back(x);
}
std::cout<<"Numbers of values read: "<<values.size()<<'\n';
//Loop over all the elements and print the number of negative elements found.
int count=0;
for(auto i=values.cbegin(); i!=values.cend(); ++i)
{
if(*i==n)
{
++ count;
}
}
cout<<"The number of times "<<n<<" appears in the vector is: "<<count<<" times"<<'\n';
return 0;
}
Comments
Leave a comment