3)
#include <iostream>
#include <iterator>
#include <vector>
template <typename InputIterator, typename OutputIterator>
OutputIterator Copy(InputIterator first, InputIterator last, OutputIterator result)
{
while(first != last)
*result++ = *first++;
return result;
}
template <typename InputIterator, typename Predicate>
unsigned Count_if(InputIterator first, InputIterator last, Predicate pred)
{
unsigned Cnt = 0;
while(first != last)
if(pred(*first++))
Cnt++;
return Cnt;
}
template <typename InputIterator, typename Predicate>
unsigned Accumulate(InputIterator first, InputIterator last, Predicate pred)
{
unsigned Sum = 0;
while(first != last)
{
if(pred(*first))
Sum += *first;
first++;
}
return Sum;
}
int main()
{
std::vector <int> vec;
std::cout<<"Enter vector"<<std::endl;
Copy(std::istream_iterator<int>(std::cin), std::istream_iterator <int>(), back_inserter(vec));
std::cout<<"Input vector"<<std::endl;
Copy(vec.begin(), vec.end(), std::ostream_iterator <int> (std::cout, " "));
std::cout<<std::endl<<"Count negative elements = ";
std::cout<<Count_if(vec.begin(), vec.end(), bind2nd(std::less<int>(), 0));
std::cout<<std::endl<<"Sum positive elements = ";
std::cout<<Accumulate(vec.begin(), vec.end(), bind2nd(std::greater<int>(), 0))<<std::endl;
system("pause");
return 0;
}
Терранова
пікір