My question is regaring STL iterator/reverse_iterato r.
I would like to write a function which does the following
int CFoo::calculate (std::vector<in t>::iterator itBegin,
std::vector<int >::iterator itEnd)
{
int nWSum = 0;
std::vector<int >::iterator it = itBegin;
for (int i = 1; it != itEnd; ++it, ++i)
{
nWSum += i*(*it);
}
return nWSum;
}
Ideally, I would like to pass in the forward iterator or the reverse
iterator for the vector (the weighted sum will be different for each).
int nSum1 = oCFoo.calculate (vecA.begin(), vecA.end());
int nSum2 = oCFoo.calculate (vecA.rbegin(), vecA.rend());
How should I setup the interface for calculate so that I can do that?
Thanks.
I would like to write a function which does the following
int CFoo::calculate (std::vector<in t>::iterator itBegin,
std::vector<int >::iterator itEnd)
{
int nWSum = 0;
std::vector<int >::iterator it = itBegin;
for (int i = 1; it != itEnd; ++it, ++i)
{
nWSum += i*(*it);
}
return nWSum;
}
Ideally, I would like to pass in the forward iterator or the reverse
iterator for the vector (the weighted sum will be different for each).
int nSum1 = oCFoo.calculate (vecA.begin(), vecA.end());
int nSum2 = oCFoo.calculate (vecA.rbegin(), vecA.rend());
How should I setup the interface for calculate so that I can do that?
Thanks.
Comment