Need help on remove_if

W

Wang Tong

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;
}
 
S

Sam Holden

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).
 
S

Stewart Gordon

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:
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.
 
C

Chris \( Val \)

| 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
 
T

Thomas Wintschel

Stewart Gordon said:
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:


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.
 
S

Stewart Gordon

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:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top