Bo Yang
11/20/2008 1:24:00 AM
On Nov 20, 12:25 am, Pete Becker <p...@versatilecoding.com> wrote:
> On 2008-11-19 09:22:25 -0500, Victor Bazarov <v.Abaza...@comAcast.net> said:
>
>
>
> > James Kanze wrote:
> >> On Nov 19, 3:35 am, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
> >>> Bo Yang wrote:
>
> >> [...]
> >>>> And when I use the remove_if and many other algorithm on
> >>>> set, it will make some crash, why?
>
> >>> Don't know. Show the code, then we can talk.
>
> >> It shouldn't compile, if your library implementation and
> >> compiler are conformant. You can't modify members of a set,
> >> which means that none of the mutating algorithms can be used on
> >> it. remove_if is a mutating algorithm.
>
> > It's mutating to the sequence, not to the elements. Removing from a
> > set is well-defined and it keeps the set in stable state, doesn't it?
>
> remove_if, like all the standalone algorithms, operates on the elements
> of a sequence and not on the underlying data structure. That's the
> essential abstraction that iterators present. Think of how remove_if
> would work on a vector: copy elements downward, but don't copy elements
> that should be removed.
>
> The key thing here is that algorithms operate on sequences, not on
> containers. Containers are a way of producing sequences, but algorithms
> don't know where the sequence came from, so cannot apply operations
> like removing an element from a set. And that's why list has its own
> sort member function: the generic sort algorithm doesn't work for a
> list, so you need a container-specific sort function.
Ah, that is a good point. If STL algorithms operate on sequences, I
understand why my program failing. Thanks!
Regards!
Bo