Boost logo

Boost :

Subject: Re: [boost] On Iteration
From: Scott McMurray ([hidden])
Date: 2009-11-11 20:17:10

2009/11/11 Christian Holmquist <c.holmquist_at_[hidden]>:
> How would the following example from the SGI STL doc be written in, say, D's
> standard library (I did search the D documentation, but couldn't find the
> answer).
>    std::vector<int> v;
>    v.push_back(...);
>    v.erase(v.begin(), std::find(v.begin(), v.end(), X));

That's my biggest complaint with ranges so far. Similar problems
exist if you want to get the range of elements before the pivot out of
a partition.

For this particular case, the usual answer is that you should be using
a sentinel-terminated range instead of the find, something like this:

    auto before_part = before(v.all(), X);

But as you point out, it's unreasonable for vector to have to know
about a before_range to be able to handle that, so near as I can tell
you're stuck with using slice:

    auto before_part = v.all().slice(0, v.size()-find(v.all(), X).size());

Which is quite ugly and error-prone, not to mention the obvious
complaint that it requires a random-access, finite range for something
that's doable with just a forward iterator.

I've been musing about whether something iterator-like, but opaque, à
la fgetpos/fsetpos, would be a feasible solution, but haven't worked
out the details.

Bright ideas welcome,
~ Scott McMurray

Boost list run by bdawes at, gregod at, cpdaniel at, john at