Topic: STL find algo


Author: Matt Austern <austern@isolde.mti.sgi.com>
Date: 1998/02/06
Raw View
"Robin Hansen" <roha@ita.cph.dk> writes:

> I suppose what one needs to do is:
> ...
> list<int>::iterator iter = find( mylist.begin(), mylist.end(), 3);
> if ( iter != mylist.end() )
>  mylist.erase( find( mylist.begin(), mylist.end(), 3) );
>

That would work, if what you want to do is remove the first "3" in the
list.  You could also use list's member function remove().



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Paul Sorensen" <psorensen@bigpond.com>
Date: 1998/02/05
Raw View
Robin Hansen wrote in message <01bd315a$7f176820$efd5640a@itaroha.cph.dk>...
>What *should* be the effect of :
>
> list<some_type> mylist;
> mylist.erase( mylist.begin(), mylist.end(), *(mylist.end()) );

I would imagine that it *should* do anyone of:

a) cause a memory exception mylist.end() points past the process's available
memory.
b) delete some random elements of mylist that happen to match what is
pointed to by mylist.end()
c) in most cases, nothing at all.  This is where mylist.end() points to an
unused part of the allocator's allocated memory, but the contents (when
treated as an object of <some_type>) don't match any of the elements in
mylist.

Regards
Paul Sorensen
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: "Bradd W. Szonye" <bradds@concentric.net>
Date: 1998/02/05
Raw View
Robin Hansen wrote in message
<01bd315a$7f176820$efd5640a@itaroha.cph.dk>...
>
>What *should* be the effect of :
>
> list<some_type> mylist;
> mylist.erase( mylist.begin(), mylist.end(), *(mylist.end()) );
>
>Why such a silly question? Because this is the effect of trying to
erase an
>element which is not in the list.

I'm not sure what you mean by this, unless your STL implements a version
of list<> different from the one in CD2. The parameters of the official
list<> are one or two iterators, corresponding to an element or range of
elements, respectively.

Given:
list<some_type> mylist;
list<some_type>::iterator myiter;
mylist.erase(myiter);

... I would expect undefined behavior if myiter is uninitialized or
pointed to some other list--undefined behavior, not a no-op. You're
violating the preconditions for list::erase(). If that's not what you're
doing, please clarify so that we can give a more appropriate answer.

Bradd W. Szonye
bradds@concentric.net
http://www.concentric.net/~Bradds



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Robin Hansen" <roha@ita.cph.dk>
Date: 1998/02/06
Raw View
Sorry, I was typing away a bit to quickly, here is a correction to my
previous posting. I think the whole point was missed.
What I meant should be clarified by the following example:

list<int> mylist;
mylist.push_back( 1 );
mylist.push_back( 2 );
mylist.erase( find( mylist.begin(), mylist.end(), 3) );

This leaves mylist invalid (HP STL '94), because find(...) obviously does
not find 3 in the list, and therefore returns mylist.end()

I suppose what one needs to do is:
...
list<int>::iterator iter = find( mylist.begin(), mylist.end(), 3);
if ( iter != mylist.end() )
 mylist.erase( find( mylist.begin(), mylist.end(), 3) );

Only had I hoped that mylist.erase( mylist.end() ) would be a no-op.


Regards,
Robin
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: "Robin Hansen" <roha@ita.cph.dk>
Date: 1998/02/04
Raw View
Does anyone have comments to the following (and I apologize if this has
been discussed before... )

What *should* be the effect of :

 list<some_type> mylist;
 mylist.erase( mylist.begin(), mylist.end(), *(mylist.end()) );

Why such a silly question? Because this is the effect of trying to erase an
element which is not in the list. I had anticipated it to be a no-op,
instead the list is invalidated such that a subsequent 'for_each' loops
forever. Any comments out there?  Beforehand, I better admit, I use the '94
HP version of STL  (Had compilation problems with SGI's on with XlC on AIX
4.2)

Regards,

Robin Edgar Hansen
IT consultant, Copenhagen Airports
Denmark
e-mail: roha@ita.cph.dk
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]