By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,227 Members | 1,176 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,227 IT Pros & Developers. It's quick & easy.

Need help on remove_if

P: n/a
I have the following code, which removes a pair (string, int) from the
set. I defined a predicate that returns true if the string value matches.

But I am getting compiler error on the remove_if function:

Here is the code:

typedef pair<string, int> my_pair;
typedef set<my_pair> my_set;
typedef my_set::iterator my_set_p;

class my_pair_eq : public unary_function<my_pair, bool>
{
string s;
public:
explicit my_pair_eq (const string& ss) : s(ss) { }
bool operator() (const my_pair& mp) const {return mp.first == s; }
};
int main(void)
{
my_set mset;

mset.insert(my_pair("one", 1));
mset.insert(my_pair("one", 2));
mset.insert(my_pair("two", 2));
mset.insert(my_pair("one", 3));
mset.insert(my_pair("three", 3));

remove_if(mset.begin(), mset.end(), my_pair_eq("one"));

return 0;
}

Jul 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Sun, 07 Dec 2003 23:22:46 -0500, Wang Tong <wa******@seas.upenn.edu> wrote:
I have the following code, which removes a pair (string, int) from the
set. I defined a predicate that returns true if the string value matches.

But I am getting compiler error on the remove_if function:


You can't use remove_if on a set. It is a mutating algorithm (ie. it
makes changes to the underlying container), but set doesn't allow you
to do that.

It also makes no sense. remove_if, moves the items in the range that
satisfy the predicate to the end of the range (and returns a new end
iterator indicating where those "removed" elements start). A set is
ordered and hence you can't rearrange the elements (since it would then
no longer be ordered).

--
Sam Holden
Jul 22 '05 #2

P: n/a
While it was 8/12/03 4:56 am throughout the UK, Sam Holden sprinkled
little black dots on a white screen, and they fell thus:
<snip>
It also makes no sense. remove_if, moves the items in the range that
satisfy the predicate to the end of the range (and returns a new end
iterator indicating where those "removed" elements start). A set is
ordered and hence you can't rearrange the elements (since it would then
no longer be ordered).


Correction:

A set is unordered, i.e. there is no such thing as the order of elements
within a set.

If, in normal mathematical notation, we write

S = { 3, 7, 12 }

then this is merely a way of writing it down - as much as 7 appears to
be between 3 and 12, it isn't. We could write the same set as

S = { 7, 12, 3 }

A list is ordered, i.e. the order of elements is defined. So <7, 3, 12>
and <12, 3, 7> are two distinct lists.

Of course, one might use a list in order to implement a set, and may
even automatically sort the underlying list giving a clear order in
which an iterator returns elements. But this order is not part of the
set - it is just provided for the purpose of being able to e.g. search a
set or operate on each element in turn.

Stewart.

--
My e-mail is valid but not my primary mailbox, aside from its being the
unfortunate victim of intensive mail-bombing at the moment. Please keep
replies on the 'group where everyone may benefit.
Jul 22 '05 #3

P: n/a

"Wang Tong" <wa******@seas.upenn.edu> wrote in message
news:br***********@netnews.upenn.edu...
| I have the following code, which removes a pair (string, int) from the
| set. I defined a predicate that returns true if the string value matches.
|
| But I am getting compiler error on the remove_if function:

[snip]

| int main(void)
| {
| my_set mset;
|
| mset.insert(my_pair("one", 1));
| mset.insert(my_pair("one", 2));
| mset.insert(my_pair("two", 2));
| mset.insert(my_pair("one", 3));
| mset.insert(my_pair("three", 3));
|
| remove_if(mset.begin(), mset.end(), my_pair_eq("one"));

mset.erase( std::remove_if( mset.begin(),
mset.end(), my_pair_eq("one") ), mset.end() );

Cheers.
Chris Val
Jul 22 '05 #4

P: n/a

"Stewart Gordon" <sm*******@yahoo.com> wrote in message
news:br********@sun-cc204.lut.ac.uk...
While it was 8/12/03 4:56 am throughout the UK, Sam Holden sprinkled
little black dots on a white screen, and they fell thus:
<snip>
It also makes no sense. remove_if, moves the items in the range that
satisfy the predicate to the end of the range (and returns a new end
iterator indicating where those "removed" elements start). A set is
ordered and hence you can't rearrange the elements (since it would then
no longer be ordered).
Correction:

A set is unordered, i.e. there is no such thing as the order of elements
within a set.

If, in normal mathematical notation, we write

S = { 3, 7, 12 }

then this is merely a way of writing it down - as much as 7 appears to
be between 3 and 12, it isn't. We could write the same set as

S = { 7, 12, 3 }


In mathematical terminology, yes. In the standard library, however,
std::set is ordered, std::list is not.
A list is ordered, i.e. the order of elements is defined. So <7, 3, 12>
and <12, 3, 7> are two distinct lists.

Of course, one might use a list in order to implement a set, and may
even automatically sort the underlying list giving a clear order in
which an iterator returns elements. But this order is not part of the
set - it is just provided for the purpose of being able to e.g. search a
set or operate on each element in turn.

Stewart.

--
My e-mail is valid but not my primary mailbox, aside from its being the
unfortunate victim of intensive mail-bombing at the moment. Please keep
replies on the 'group where everyone may benefit.

Jul 22 '05 #5

P: n/a
While it was 8/12/03 2:52 pm throughout the UK, Thomas Wintschel
sprinkled little black dots on a white screen, and they fell thus:
<snip>
In mathematical terminology, yes. In the standard library, however,
std::set is ordered, std::list is not.

<snip>

You mean the STL bible uses the word "ordered" with a meaning contrary
to the mathematical one? That sense corresponds to the word "sorted" in
my vocabulary.

Stewart.

--
My e-mail is valid but not my primary mailbox, aside from its being the
unfortunate victim of intensive mail-bombing at the moment. Please keep
replies on the 'group where everyone may benefit.
Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.