473,666 Members | 2,048 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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::iterato r 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 1849
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.upe nn.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*******@yaho o.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
2435
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 last year we got traffic of between 2000 - 2500 unique visitors per day. We are now about to re-launch the site from Sweden and we need to purchase a script to run it. Having looked at what is available on the net I have realised that we need a...
4
5555
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. Thanks in advance. /usr/include/c++/3.2.3/bits/stl_algo.h: In function `_OutputIter std::remove_copy_if(_InputIter, _InputIter, _OutputIter, _Predicate) ': /usr/include/c++/3.2.3/bits/stl_algo.h:1085: instantiated from `_ForwardIter...
1
5174
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 code..
2
2448
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> #include <vector>
0
1493
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 elements reference using erase/remove_if idiom. //////// #include <algorithm>
2
1953
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. Now I want to remove all "Patch"es in the vector dataPatches, whose outlier variable is set to true.
0
3946
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 Inbox Reply from Craig Somerford <uscos@2barter.net> hide details 10:25 pm (3 minutes ago)
3
4143
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, but not what we want - we cant access m_Exclustions
3
2581
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: class is_finished : public std::unary_function<CRequest, bool> {
0
8444
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
8551
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8639
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7386
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—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6198
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5664
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4198
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4368
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2771
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 we have to send another system

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.