L
learning_C++
Hi,
I compiled some code which includs times<int>(). The compiler says
"times' undeclared (first use this function)".
I also included the header <functional> in my code.
But After I replaced "times<int>()" with "multiplies<int>()" it works.
What is wrong with my code?
Thanks,
#include <numeric>
#include <vector>
#include <functional>
#include <iostream>
using namespace std;
int main()
{
vector<int>::iterator iter;
int d1[10] = {1,2,3,4,5,6,7,8,9,10};
vector<int> v1(d1, d1+10);
int sum = accumulate(v1.begin(), v1.end(), 10);
int prod = accumulate(v1.begin(), v1.end(),
1, times<int>());
cout << "For the series: ";
for(iter = v1.begin(); iter != v1.end(); iter++)
cout << *iter << " ";
cout << " where N = 10." << endl;
cout << "The sum = (N*N + N)/2 = " << sum << endl;
cout << "The product = N! = " << prod << endl;
return 0;
}
I compiled some code which includs times<int>(). The compiler says
"times' undeclared (first use this function)".
I also included the header <functional> in my code.
But After I replaced "times<int>()" with "multiplies<int>()" it works.
What is wrong with my code?
Thanks,
#include <numeric>
#include <vector>
#include <functional>
#include <iostream>
using namespace std;
int main()
{
vector<int>::iterator iter;
int d1[10] = {1,2,3,4,5,6,7,8,9,10};
vector<int> v1(d1, d1+10);
int sum = accumulate(v1.begin(), v1.end(), 10);
int prod = accumulate(v1.begin(), v1.end(),
1, times<int>());
cout << "For the series: ";
for(iter = v1.begin(); iter != v1.end(); iter++)
cout << *iter << " ";
cout << " where N = 10." << endl;
cout << "The sum = (N*N + N)/2 = " << sum << endl;
cout << "The product = N! = " << prod << endl;
return 0;
}