Understanding Iterator Question

Hello,
I don't understand this question in the book I'm reading.
Write a function void copy(int* f1, int* e1, int* f2) that copies the elements of an array of ints defined by [f1:e1) into another [f2:f2+(el-fl)). Use only the iterator operations mentioned above (not subscripting).

If someone could explain [f1:e1) and [f2:f2+(el-fl)) I think I could solve this problem.
I understand '(' excludes and '[' includes but I don't understand the combinations of two different iterators. I usually use them in math for sequencing but it seems a little different in programming.
Last edited on
In fact it is standard algorithm std::copy except it has return type of the second iterator instead of void as in your function.

So the body of the function is simple

1
2
3
4
void copy( int *first, int *last, int *out )
{
   while ( first != last ) *out++ = *first++;
}
Cool
But could you explain [f1:e1) and [f2:f2+(el-fl)).
How does that explain copy f1 array into f2 from f1 through e1.
Thanks
[f1:e1) sets a range of iterators that incluudes f1 and excludes e1. Moreover pointer e1 is reachable from pointer f1. It means that if we would increase f1 then after some steps f1 will become equal to e1.
So how many steps do we need that f1 become equal to e1? This number is e1 - f1. So when f1 will be changed from its initial value f1 to end value e1 at the same time f2 will be changed from its initial value f2 to f2 + ( e1 - f1 ).
Thank you very much I believe I understand it.

So [f1:e1) means: f1 to e1 including f1 but excluding e1 and [f2:f2+(el-fl)) means: inclusive f2 to exclusive f2 plus the difference of f1 from e1.

I was having problems because [f2:f2+(el-fl)) seemed so convoluted.
Topic archived. No new replies allowed.