StephQ a écrit :
> I need to implement an algorithm that takes as input a container and
> write some output in another container.
> The containers involved are usually vectors, but I would like not to
> rule out the possibility of using lists.
> The problem is that I need two versions of it, depending if I'm adding
> the generated (by the algorithm) values to the target container or if
> I just modify pre-existing values of the target container.
> Efficiency is important in this part of the software.
> The first solution is to write two versions of the algorithm:
> [firstTime, lastTime) define the input range, and firstTarget define
> the begin of target container.
[snip]
> The problem is then:
> I need to pass the iterator to the starting element of the target
> container to cover the Modify case.
> But then I can't figure out how to invoke the push_back() function to
> the target container starting from this iterator. I suspect I can't.
> What would be perfect would be the ability to deference the iterator
> two times to get the actual container but I think you can't do
> this....
> Is there a way to solve this situation using a single function for the
> algorithm?
Yes. You can use the std::copy algorithm of the STL.
When you want to overwrite, you just use plain iterators; by example:
copy(from.begin(),from.end(),to.begin());
When, you want to append, you use a back_inserter:
copy(from.begin(),from.end(), back_insert_iterator< list<int> >(to));
Michael