473,378 Members | 1,415 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Need help on remove_if

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
5 1836
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
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

"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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Sofia | last post by:
My name is Sofia and I have for many years been running a personals site, together with my partner, on a non-profit basis. The site is currently not running due to us emigrating, but during its...
4
by: CupOfWater | last post by:
Hi, I'm getting some errors using remove_if that I can not fix at all. Let me paste my code and also the error here. I went on IRC and people were mostly no help. The compiler is g++ 3.2.3. ...
1
by: Paavo K | last post by:
Hi, can anyone tell what is wrong with following code : //..Code class removeTest { public: bool operator()(map<int, string>::value_type &d) const { // Just returning false to simplify...
2
by: marco_segurini | last post by:
Hi, if I compile the following code I get the error: RemoveIf.cpp(19) : error C2914: 'remove_if' : cannot deduce template argument as function argument is ambiguous #include <algorithm>...
0
by: marco_segurini | last post by:
Hi, I am trying to solve this problem: I have a vector V1 that is not empty and a vector V2 that contains some iterator that point to V1 elements. Now I want to remove the V1 elements that V2...
2
by: Adam Hartshorne | last post by:
Hi All, I want to do the following, I have a std::vector<Patch> called say vs. Now there is a bool variable in the class called outlier, which may be access by getOutlierStatus() method. ...
0
by: U S Contractors Offering Service A Non-profit | last post by:
Brilliant technology helping those most in need Inbox Reply U S Contractors Offering Service A Non-profit show details 10:37 pm (1 hour ago) Brilliant technology helping those most in need ...
3
by: Jon Rea | last post by:
Hello all, Sorry if this is long, but I wanted to be specific... Can anyone shed any light on the following errors in the context of the following example code: cullMethod1(); // compiles,...
3
by: Angus | last post by:
Hi I have a class CRequest with a function: bool IsFinished() const; I have a list of these CRequests - list<CRequestmylist. I create a function object to find if a CRequest is finished:...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.