C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Re: vector erase

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
P.J. Plauger
Guest





PostPosted: Fri Jan 27, 2006 12:00 pm    Post subject: Re: vector erase Reply with quote



"Amadeus W. M." <amadeus84 (AT) cablespeed (DOT) com> wrote


Quote:
On Thu, 26 Jan 2006 20:54:38 -0500, Amadeus W. M. wrote:

I have a vector from which I want to erase elements between iterators
i,j.
If i<j, everything works as expected, but if j>i, an insertion is
actually
performed. Example:

vector<double> x(10);
vector<double>::iterator i=x.begin()+2, j=x.begin()+6;

x.erase(i,j); // i<j, ok, erases 4 elements.
x.erase(j,i); // j>i, no error, just inserts 4 elements.

This happens with g++ 4.0.2. Is this standard behavior? Why does it work
this way?


After looking at the code, it's no wonder that the result looks like an
insertion. In fact, I only tried on a vector with all elements the same.
On a random vector it won't look like an insertion.

I just wanted to know whether or not it was standard. It would have been
nice to throw an exception or something. Here is the code:

template<typename _Tp, typename _Alloc
typename vector<_Tp, _Alloc>::iterator
vector<_Tp, _Alloc>::
erase(iterator __first, iterator __last)
{
iterator __i(std::copy(__last, end(), __first));
std::_Destroy(__i, end(), this->get_allocator());
this->_M_impl._M_finish = this->_M_impl._M_finish - (__last -
__first);
return __first;
}

Very informative this open source thing!

Yeah, one of the benefits of template libraries is that they pretty
much have to show all their code in publicly readable headers.
(Export templates haven't quite caught on yet, if they every will.)
So if by "open source" you mean code that you can see, then *all*
implementations of the Standard C++ library are pretty open, at
least the STL part.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com



Back to top
Stefan Näwe
Guest





PostPosted: Fri Jan 27, 2006 12:13 pm    Post subject: [OT] Re: vector erase Reply with quote



P.J. Plauger schrieb:
Quote:
Very informative this open source thing!

Yeah, one of the benefits of template libraries is that they pretty
much have to show all their code in publicly readable headers.
(Export templates haven't quite caught on yet, if they every will.)
So if by "open source" you mean code that you can see, then *all*
implementations of the Standard C++ library are pretty open, at
least the STL part.

RMS would love this!
This shows exactly the difference between "Open Source" and "Free As in Speech" software.

/S
--
Stefan Naewe
naewe.s_AT_atlas_DOT_de

Back to top
Pete Becker
Guest





PostPosted: Fri Jan 27, 2006 2:17 pm    Post subject: Re: vector erase Reply with quote



loquacious wrote:

Quote:

Below is one the implementation of vector::erase() which I thought
might be usefull in understanding the behaviour.


To what end? The behavior is undefined, so whatever some implementation
does is simply what that implementation does. If you want to rely on
that behavior, so be it. But that's dangerous here, because conceptually
what's being done doesn't make sense, since the range to be erased isn't
a valid range.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.