473,480 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

std::list remove element mid iteration

The situation is that a std::list<std::set<std::string is being
iterated through. Upon certain criteria some sets become empty. I need
to remove the empty sets from the list.

Is it safe to iterate through a list and call list::erase( iterator )
in mid iteration?
Dec 20 '07 #1
5 8633
Christopher wrote:
The situation is that a std::list<std::set<std::string is being
iterated through. Upon certain criteria some sets become empty. I need
to remove the empty sets from the list.

Is it safe to iterate through a list and call list::erase( iterator )
in mid iteration?
Well, you can use

struct set_is_empty
{
bool operator()(const std::set& s) const { return s.empty(); }
};

std::erase(std::remove_if(l.begin(), l.end(), set_is_empty());

or else, the canonical iteration for this list is:

for (it = l.begin(); it != l.end(); )
if (it->empty())
it = l.erase(it);
else
++it;
Dec 20 '07 #2
red floyd wrote:
Christopher wrote:
>The situation is that a std::list<std::set<std::string is being
iterated through. Upon certain criteria some sets become empty. I need
to remove the empty sets from the list.

Is it safe to iterate through a list and call list::erase( iterator )
in mid iteration?

Well, you can use

struct set_is_empty
{
bool operator()(const std::set& s) const { return s.empty(); }
};

std::erase(std::remove_if(l.begin(), l.end(), set_is_empty());
crap. That should be:

l.erase(std::remove_if(l.begin(), l.end(), set_is_empty()), l.end());
>
or else, the canonical iteration for this list is:

for (it = l.begin(); it != l.end(); )
if (it->empty())
it = l.erase(it);
else
++it;
Dec 20 '07 #3
On Dec 20, 2:10 am, red floyd <no.s...@here.dudewrote:
Christopher wrote:
The situation is that a std::list<std::set<std::string is being
iterated through. Upon certain criteria some sets become empty. I need
to remove the empty sets from the list.
Is it safe to iterate through a list and call list::erase( iterator )
in mid iteration?
Well, you can use
struct set_is_empty
{
bool operator()(const std::set& s) const { return s.empty(); }
};
std::erase(std::remove_if(l.begin(), l.end(), set_is_empty());
Which could be unnecessarily expensive. In the case of
std::list, the canonical form is:

l.remove_if( set_is_empty() ) ;

However, the original poster said that sets "become" empty
during his iteration, so this can't be used.
or else, the canonical iteration for this list is:
for (it = l.begin(); it != l.end(); )
if (it->empty())
it = l.erase(it);
else
++it;
Adopted to his case, you'd add braces and put the if at the end
of the loop. (Also, I'd write this with a while, rather than a
for. Something like:

std::list<...>::iterator iter = l.begin() ;
while ( iter != l.end() ) {
// processing...
if ( iter->empty() ) {
iter = l.erase( iter ) ;
} else {
++ iter ;
}
}

I'd prefer even more if that if could be replaced with a ?: on
the right side of an assignment, since the most important aspect
here is the update of the iterator, and not how it's being
updated, but I can't think of a nice way of doing this off hand.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orient閑 objet/
Beratung in objektorientierter Datenverarbeitung
9 place S閙ard, 78210 St.-Cyr-l'蒫ole, France, +33 (0)1 30 23 00 34
Dec 20 '07 #4
On 12月20日, 下午5时40分, James Kanze <james.ka...@gmail.comwrote:
On Dec 20, 2:10 am, red floyd <no.s...@here.dudewrote:
Christopher wrote:
The situation is that a std::list<std::set<std::string is being
iterated through. Upon certain criteria some sets become empty. I need
to remove the empty sets from the list.
Is it safe to iterate through a list and call list::erase( iterator )
in mid iteration?
Well, you can use
struct set_is_empty
{
bool operator()(const std::set& s) const { return s.empty(); }
};
std::erase(std::remove_if(l.begin(), l.end(), set_is_empty());

Which could be unnecessarily expensive. In the case of
std::list, the canonical form is:

l.remove_if( set_is_empty() ) ;
That is what I found in MSDN,
remove_if is a STL algorithm which removes all elements from the range
(First,Last) that cause the predicate to return true. It returns an
iterator equal to Last - n, where n = number of elements removed. The
last n elements of the range have undefined values. The size of the
container remains the same.
But there is a method named remove_if in std::list.
template<class Predicate>
void remove_if(
Predicate _Pred
)
Erases elements from a list for which a specified predicate is
satisfied.
I didn't know there is a method named remove_if in the std::list. Can
any one told me why there is no similar method in vector ?

Thanks in advance,:)
Dec 20 '07 #5
On Dec 20, 12:40 pm, "cgsp...@gmail.com" <cgsp...@gmail.comwrote:
On 12鏈20鏃, 涓嬪崍5鏃40鍒, James Kanze <james.ka...@gmail.comwrote:
On Dec 20, 2:10 am, red floyd <no.s...@here.dudewrote:
Christopher wrote:
The situation is that a std::list<std::set<std::string is being
iterated through. Upon certain criteria some sets become empty. I need
to remove the empty sets from the list.
Is it safe to iterate through a list and call list::erase( iterator )
in mid iteration?
Well, you can use
struct set_is_empty
{
bool operator()(const std::set& s) const { return s.empty(); }
};
std::erase(std::remove_if(l.begin(), l.end(), set_is_empty());
Which could be unnecessarily expensive. In the case of
std::list, the canonical form is:
l.remove_if( set_is_empty() ) ;
That is what I found in MSDN,
remove_if is a STL algorithm which removes all elements from the range
(First,Last) that cause the predicate to return true. It returns an
iterator equal to Last - n, where n = number of elements removed. The
last n elements of the range have undefined values. The size of the
container remains the same.
But there is a method named remove_if in std::list.
template<class Predicate>
void remove_if(
Predicate _Pred
)
Erases elements from a list for which a specified predicate is
satisfied.
I didn't know there is a method named remove_if in the std::list. Can
any one told me why there is no similar method in vector ?
Because you don't need it, and it's not directly supported by
the underlying data structure.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orient茅e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S茅mard, 78210 St.-Cyr-l'脡cole, France, +33 (0)1 30 23 00 34
Dec 20 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
6607
by: Martin Magnusson | last post by:
I know a similar question was recently posted here, but after trying the solutions in that thread I still have problems. list<int> the_list; the_list.push_back(1); the_list.push_back(2);...
3
9292
by: scooter | last post by:
std::list::clear() can slow down the performance of a method considerably if it is called many times a second. I have countered this problem a little by assigning a fixed size to a list when...
2
4747
by: Morten Aune Lyrstad | last post by:
Hi! I haven't been using the standard template libraries much. I have a hard time trying to figure out just what is the practical difference between std::vector and std::list. Aren't both basically...
11
2240
by: William Payne | last post by:
Ok, in my program I have a std::list<Document*>, where Document is one of my own classes. I need to go through this list and check each item if it's ready for deletion. If it's not, skip to...
3
2116
by: Jef Driesen | last post by:
The number of items in my std::list is changed after sorting (using std::list member function sort). I'm using MSVC6 and the actual (pseudo) code is posted below. The output from size() is...
3
3708
by: Andy | last post by:
Hello, I have the following situation: Thread A is allocating a dataset, doing some low-level calculations and storing a pointer to the dataset in a std::list via push_back. Thread B should...
7
2148
by: SpreadTooThin | last post by:
I want to replace an object in a list with a new object.... std::list<myObj>::iterator it; for (it=mylist.begin(); it != mylist.end(); ++it) { if (it->compair(myval) == 0) { *it = newval;...
2
2998
by: raven.mp4 | last post by:
Hi, I have a little question about std::list iterators. Take a look at this piece of code: list<Type*>::iterator it = myList.begin(); Type *p; for(it; it != myList.end(); ++it) {
0
1702
by: Javier | last post by:
Hi all, I have this code: class A { std::list<Bm_observadores; void function() {
0
7037
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6904
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7032
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
5321
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project梡lanning, coding, testing,...
0
4471
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2976
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1294
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
558
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
174
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.