K
Kaba
Hi,
Problem: Given is a forward iterator range [begin, end[ that we know is
200 elements long and where iterators dereference to int. Fill the first
100 elements with the value 1, and the next 100 elements with value 2.
Sounds easy, right? Let's start with those 1's:
std::fill_n(begin, 100, 1);
But now, we don't know the starting iterator for the 2's. We need to do:
std::advance(begin, 100);
and only after then:
std::fill_n(begin, 100, 2);
While this works, the std::advance function can spend 100 increments to
move a pure forward iterator, all work that is actually already being
done inside fill_n, but then discarded.
Any idea how to avoid using std::advance?
I think this could be solved by implementing somekind of reference
iterator which would redirect all functionality to a member iterator
which is hold by reference. Maybe Boost has something like this?
Problem: Given is a forward iterator range [begin, end[ that we know is
200 elements long and where iterators dereference to int. Fill the first
100 elements with the value 1, and the next 100 elements with value 2.
Sounds easy, right? Let's start with those 1's:
std::fill_n(begin, 100, 1);
But now, we don't know the starting iterator for the 2's. We need to do:
std::advance(begin, 100);
and only after then:
std::fill_n(begin, 100, 2);
While this works, the std::advance function can spend 100 increments to
move a pure forward iterator, all work that is actually already being
done inside fill_n, but then discarded.
Any idea how to avoid using std::advance?
I think this could be solved by implementing somekind of reference
iterator which would redirect all functionality to a member iterator
which is hold by reference. Maybe Boost has something like this?