Exceptional coding style

Stewart Gordon smjg_1998 at yahoo.com
Tue Jan 15 13:13:44 PST 2013


On 15/01/2013 07:24, Tavi Cacina wrote:
<snip>
> That piece of code is correct.
> http://en.wikipedia.org/wiki/Erase-remove_idiom

I must have misread the documentation on cplusplus.com.  I'd somehow 
made out that it returns an iterator that skips over the "removed" values.

So the semantics of remove are counter-intuitive, and what it actually 
does is to change something like [1, 3, 5, 5, 7, 5, 9] into [1, 3, 7, 9, 
RG, RG, RG] (RG = random garbage), and return an iterator that points to 
the first RG, and then erase is used to delete the elements from that 
point to the end of the array.  I see now.

But it goes to show that std::remove is counter-intuitve in itself, and 
that there ought to be a more intuitive way in STL of doing a 
search-and-remove operation.

Still, there's nothing to stop you from writing a function wrapper for 
this boilerplate code rather than writing it out every time.

Stewart.



More information about the Digitalmars-d mailing list