473,657 Members | 3,041 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

binary_function and bind1st

I have an application where I want to remove all of the items that are
in one vector from a second vector. In the following short program, my
objective is to remove all of ourCards from cardsAvailable without
writing a loop.

I have tried a number of different variations using the for_each
algorithm without success, and am currently getting a C2664 error that
the compiler cannot convert parameter 1. I am new to STL and not all
that adept with C++ either, so I'm sure there is something basic that
I am missing. Anyway, I have created the following short program to
demonstrate my problem. Any assistance will be greatly appreciated.

// Remove Card test using STL

#include <iostream> // for cout
#include <vector> // for vector
#include <algorithm> // for lower_bound, for_each
#include <functional> // for binary_function
using namespace std;
//STL support functions or objects

struct removeCard : public std::binary_fun ction<vector<in t>,int,void>
{
void
operator() (vector<int> & cardsAvailable, int cardToRemove )const
{
vector<int>::it erator iter;
iter = lower_bound(car dsAvailable.beg in(),cardsAvail able.end(),
cardToRemove);

if ( iter != cardsAvailable. end()) cardsAvailable. erase(iter);
}
};

// To display contents of a container - For testing only
void print ( int element )
{
cout << element << ' ';
}

int main()
{
vector<int> ourCards;

// add two cards to ourCards vector
ourCards.push_b ack(10);
ourCards.push_b ack(20);

vector<int> cardsAvailable;
vector<int>::it erator iter;

// initialize cardsAvailable with all 52 cards
for ( int i = 0; i < 52; ++i) cardsAvailable. push_back(i);

// remove all of our cards from the deck
for_each( ourCards.begin( ), ourCards.end(), // range
bind1st(removeC ard(),cardsAvai lable) ); // operation

// display all of the remaining cards in the deck
for_each( cardsAvailable. begin(), cardsAvailable. end(), print );

return 0;
}

// Error message from compiler follows

/*

Compiling...
RemoveCard.cpp
C:\Program Files\Microsoft Visual Studio .NET
2003\Vc7\includ e\functional(27 9) :
error C2664: 'void removeCard::ope rator ()(std::vector< _Ty> &,int)
const' :
cannot convert parameter 1 from 'const
std::binary_fun ction<_Arg1,_Ar g2,
_Result>::first _argument_type' to 'std::vector<_T y> &'
with
[
_Ty=int
]
and
[
_Arg1=std::vect or<int>,
_Arg2=int,
_Result=void
]
and
[
_Ty=int
]
Conversion loses qualifiers
C:\Program Files\Microsoft Visual Studio .NET
2003\Vc7\includ e\functional(27 8) :
while compiling class-template member function
'std::binder1st <_Fn2>::result_ type
std::binder1st< _Fn2>::operator ()(std::binder1 st<_Fn2>::argum ent_type
&) const'
with
[
_Fn2=removeCard
]
RemoveCard.cpp( 52) : see reference to class template instantiation
'std::binder1st <_Fn2>' being compiled
with
[
_Fn2=removeCard
]

*/
Jul 22 '05 #1
6 2674
Greg Lilley wrote:
I have an application where I want to remove all of the items that are
in one vector from a second vector. In the following short program, my
objective is to remove all of ourCards from cardsAvailable without
writing a loop.

I have tried a number of different variations using the for_each
algorithm without success, and am currently getting a C2664 error that
the compiler cannot convert parameter 1. I am new to STL and not all
that adept with C++ either, so I'm sure there is something basic that
I am missing. Anyway, I have created the following short program to
demonstrate my problem. Any assistance will be greatly appreciated.

// Remove Card test using STL

#include <iostream> // for cout
#include <vector> // for vector
#include <algorithm> // for lower_bound, for_each
#include <functional> // for binary_function
using namespace std;
//STL support functions or objects

struct removeCard : public std::binary_fun ction<vector<in t>,int,void>
{
void
operator() (vector<int> & cardsAvailable, int cardToRemove )const
{
vector<int>::it erator iter;
iter = lower_bound(car dsAvailable.beg in(),cardsAvail able.end(),
cardToRemove);

if ( iter != cardsAvailable. end()) cardsAvailable. erase(iter);
}
};

// To display contents of a container - For testing only
void print ( int element )
{
cout << element << ' ';
}

int main()
{
vector<int> ourCards;

// add two cards to ourCards vector
ourCards.push_b ack(10);
ourCards.push_b ack(20);

vector<int> cardsAvailable;
vector<int>::it erator iter;

// initialize cardsAvailable with all 52 cards
for ( int i = 0; i < 52; ++i) cardsAvailable. push_back(i);

// remove all of our cards from the deck
for_each( ourCards.begin( ), ourCards.end(), // range
bind1st(removeC ard(),cardsAvai lable) ); // operation

// display all of the remaining cards in the deck
for_each( cardsAvailable. begin(), cardsAvailable. end(), print );

return 0;
}

// Error message from compiler follows

/*

Compiling...
RemoveCard.cpp
C:\Program Files\Microsoft Visual Studio .NET
2003\Vc7\includ e\functional(27 9) :
error C2664: 'void removeCard::ope rator ()(std::vector< _Ty> &,int)
const' :
cannot convert parameter 1 from 'const
std::binary_fun ction<_Arg1,_Ar g2,
_Result>::first _argument_type' to 'std::vector<_T y> &'
with
[
_Ty=int
]
and
[
_Arg1=std::vect or<int>,
_Arg2=int,
_Result=void
]
and
[
_Ty=int
]
Conversion loses qualifiers
C:\Program Files\Microsoft Visual Studio .NET
2003\Vc7\includ e\functional(27 8) :
while compiling class-template member function
'std::binder1st <_Fn2>::result_ type
std::binder1st< _Fn2>::operator ()(std::binder1 st<_Fn2>::argum ent_type
&) const'
with
[
_Fn2=removeCard
]
RemoveCard.cpp( 52) : see reference to class template instantiation
'std::binder1st <_Fn2>' being compiled
with
[
_Fn2=removeCard
]

*/


'binder1st' class requires its argument to be a _const_ reference. It
might be the reason why it can't convert what it tries to convert.

Victor
Jul 22 '05 #2
Greg Lilley wrote:

I have an application where I want to remove all of the items that are
in one vector from a second vector. In the following short program, my
objective is to remove all of ourCards from cardsAvailable without
writing a loop.

I have tried a number of different variations using the for_each
algorithm without success, and am currently getting a C2664 error that
the compiler cannot convert parameter 1. I am new to STL and not all
that adept with C++ either, so I'm sure there is something basic that
I am missing. Anyway, I have created the following short program to
demonstrate my problem. Any assistance will be greatly appreciated.

// Remove Card test using STL

#include <iostream> // for cout
#include <vector> // for vector
#include <algorithm> // for lower_bound, for_each
#include <functional> // for binary_function
using namespace std;

//STL support functions or objects

struct removeCard : public std::binary_fun ction<vector<in t>,int,void>
{
void
operator() (vector<int> & cardsAvailable, int cardToRemove )const
{
vector<int>::it erator iter;
iter = lower_bound(car dsAvailable.beg in(),cardsAvail able.end(),
cardToRemove);

if ( iter != cardsAvailable. end()) cardsAvailable. erase(iter);
}
};

// To display contents of a container - For testing only
void print ( int element )
{
cout << element << ' ';
}

int main()
{
vector<int> ourCards;

// add two cards to ourCards vector
ourCards.push_b ack(10);
ourCards.push_b ack(20);

vector<int> cardsAvailable;
vector<int>::it erator iter;

// initialize cardsAvailable with all 52 cards
for ( int i = 0; i < 52; ++i) cardsAvailable. push_back(i);

// remove all of our cards from the deck
for_each( ourCards.begin( ), ourCards.end(), // range
bind1st(removeC ard(),cardsAvai lable) ); // operation

// display all of the remaining cards in the deck
for_each( cardsAvailable. begin(), cardsAvailable. end(), print );

return 0;
}

[snip]

As Victor said, you can't use a non-const reference with bind1st.
You could do otherwise, but as a dirty cheap fix (I'm curious if anyone
thinks it's a stylistic kludge) I'd change to binding a pointer to a vector:

struct removeCard : public std::binary_fun ction<vector<in t>*, int, void> {
void operator() (vector<int>* p_cardsAvailabl e, int cardToRemove) const {
vector<int>::it erator iter;
iter = lower_bound(p_c ardsAvailable->begin(), p_cardsAvailabl e->end(),
cardToRemove);

if ( iter != p_cardsAvailabl e->end()) p_cardsAvailabl e->erase(iter);
}
};

.... and the usage in main() is:

for_each( ourCards.begin( ), ourCards.end(), // range
bind1st(removeC ard(), &cardsAvailable ) ); // operation
Denis
Jul 22 '05 #3
gr*********@dcj s.virginia.gov (Greg Lilley) wrote in message
I have an application where I want to remove all of the items that are
in one vector from a second vector. In the following short program, my
objective is to remove all of ourCards from cardsAvailable without
writing a loop.
struct removeCard : public std::binary_fun ction<vector<in t>,int,void>
The first_argument_ type is not vector<int> but rather vector<int>&.
{
void
operator() (vector<int> & cardsAvailable, int cardToRemove )const
{
vector<int>::it erator iter;
iter = lower_bound(car dsAvailable.beg in(),cardsAvail able.end(),
cardToRemove);

if ( iter != cardsAvailable. end()) cardsAvailable. erase(iter);
}
}; for_each( ourCards.begin( ), ourCards.end(), // range
bind1st(removeC ard(),cardsAvai lable) ); // operation
After changing the first_argument_ type as above, I believe you'll run
into the reference to reference problem. There may be a fix to the
ANSI standard to fix this by making a reference to a reference be a
reference.

Please be aware that bind1st creates a copy of cardsAvailable, and it
is this copy that changes. The simple fix is to use pointers. It
should also fix your original problem.

for_each( ourCards.begin( ), ourCards.end(), // range
bind1st(removeC ard(),&cardsAva ilable) ); // operation

struct removeCard : public std::binary_fun ction<vector<in t>,int,void>
should be changed to

struct removeCard : public
std::binary_fun ction<vector<in t>*,int,void>

Change operator() as appropriate.

An alternative design is to make removeCard store a reference (or
pointer) to cardsAvailable. It would have a constructor

explicit removeCard::rem oveCard(std::ve ctor<int>&);

In this design you don't need binders.
iter = lower_bound(car dsAvailable.beg in(),cardsAvail able.end(),
cardToRemove);


As a minor optimization, if ourCards is sorted, then instead of
cardsAvailable. begin() you could use the previous value of iter.
Example: for 10, 20; once you find card 10 in cardsAvailable, to find
card 20 you need only start searching cardsAvailable from card 10, not
from the beginning. Don't know how you would do this with binders
though.

Have you considered using set functions like set_difference? As far
as I know, they do employ extra storage. In other words, in

set_difference( a.begin(), a.end(), b.begin(), b.end(), c.begin());

'c' must be different from 'a'. Normally one might use
std::back_inser ter(c).

Have you thought about exception safety? What if you are to remove 2
cards 10 and 20 and you removed 10 and removing 20 throws an
exception. What then? It is not necessary to fix this scenario right
now, but at least be aware of it.

Finally, removing an element from a vector is an O(N) operation, but
removing from a list is O(1).
Jul 22 '05 #4
Thanks very much to all of you for your responses. I've been wrestling
with this problem for a long time and your comments were a big help.
An alternative design is to make removeCard store a reference (or
pointer) to cardsAvailable. It would have a constructor

explicit removeCard::rem oveCard(std::ve ctor<int>&);

In this design you don't need binders.
iter = lower_bound(car dsAvailable.beg in(),cardsAvail able.end(),
cardToRemove);

Thanks again. I was thinking there were probably some other approaches
that were superior to what I was trying to do.


As a minor optimization, if ourCards is sorted, then instead of
cardsAvailable. begin() you could use the previous value of iter.
Example: for 10, 20; once you find card 10 in cardsAvailable, to find
card 20 you need only start searching cardsAvailable from card 10, not
from the beginning. Don't know how you would do this with binders
though.
I had thought of that, but like you, I didn't know how to do it with
binders.

Have you considered using set functions like set_difference? As far
as I know, they do employ extra storage. In other words, in

set_difference( a.begin(), a.end(), b.begin(), b.end(), c.begin());

'c' must be different from 'a'. Normally one might use
std::back_inser ter(c).

No, I hadn't. I've never used set_difference before, and it hadn't
occurred to me, but it sounds like its worth trying.
Finally, removing an element from a vector is an O(N) operation, but
removing from a list is O(1).


Thanks. Speed is pretty important to me, so I'm going to think about
the list idea as well.

- Greg Lilley
Jul 22 '05 #5
gr*********@dcj s.virginia.gov (Greg Lilley) wrote in message
Finally, removing an element from a vector is an O(N) operation, but
removing from a list is O(1).


Thanks. Speed is pretty important to me, so I'm going to think about
the list idea as well.


std::deque also has removing an element as O(1)
Jul 22 '05 #6
"Siemel Naran" <na*******@exci te.com> wrote in message
news:3d******** *************** **@posting.goog le.com...
gr*********@dcj s.virginia.gov (Greg Lilley) wrote in message
Finally, removing an element from a vector is an O(N) operation, but
removing from a list is O(1).


Thanks. Speed is pretty important to me, so I'm going to think about
the list idea as well.


std::deque also has removing an element as O(1)


Only if you remove from either end. Otherwise, it's no better than
vector.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Jul 22 '05 #7

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

Similar topics

1
5285
by: wenmang | last post by:
Hi, all: I am reading Herb Sutter's article: http://www.cuj.com/documents/s=8840/cujexp0309sutter/ I have trouble to understand for following lines: class Subject { // ... public: virtual void Attach( function<void (Subject*)> o ) { obs_.push_back(o); } --Line1
2
2259
by: Fred Ma | last post by:
Hello, I have a random generator that takes a scaling factor as an argument. Since it takes one argument, it is not a generator in the sense defined by SGI's online STL documentation. I'd like to use this as a generator in the STL algorithm "generate". I know I have to change random generator from its current implementation to make it adaptable. But unlike the bind1st adapter, there isn't any premade function adapter that reduces...
6
2211
by: Marc | last post by:
T x; T foo(T, T); bind1st(ptr_fun(foo), x) creates a function object that takes an argument of type T const&. This does not work if T is already a reference type like int const&. So my first problem is with the &. My second problem is with the const. Why should bind1st change the constness of the second argument of the function ?
3
1769
by: John Black | last post by:
I have the following code trying to use bind1st, class C1{ ... }; class C2{ ... };
2
2133
by: Alberto | last post by:
Hello, while writing a program I ran across the problem of using for_each. Although I can traverse lists with a for loop, I'd prefer to use STL's for_each. Here's my faulty code: #include <iostream> #include <list> #include <algorithm>
5
6813
by: silverburgh.meryl | last post by:
I am reading an bind1st example from http://www.roguewave.com/support/docs/sourcepro/stdlibref/bind1st.html Can someone please tell me why // Even better, construct the new predicate on the fly. vector::iterator it2 = std::find_if (v1.begin (), v1.end (), std::bind1st (equal_to (), 3)); is better than
1
5801
by: pillbug | last post by:
I'm having trouble using bind1st with mem_fun1. I can't compile the line marked below with a @@. If I use the join2 method everything is fine, but I wind up passing string vectors by value. I would like to use the reference passing version, or something similar. The errors I get point the finger at binder1st::operator() being defined as "const argument_type&" which becomes "const const string_vector&&" when i use Joiner::join below. ...
4
2742
by: Christopher | last post by:
I used to just use a plain old function pointer is a call to std::sort. My colleagues are telling me that I need to use a "functor". Ok, I google and see that a functor is a class with a public method, operator () that does the comparison. Fine, no problem. What they fail to tell me is, what is the advantage to wrapping some comparison function in a class and calling it operator()? I also read that "functors" are supposed to be a...
1
2505
by: Frank Bergemann | last post by:
i don't manage to get this compiled: // shall map compare operator ids to proper std::binary_function's // for this instantiated for various types template <typename ForType> struct CompareOperator { enum Id {equal = 0, less = 1 }; // predicate ids for lookup typedef std::binary_function<ForType, ForType, boolfn_type; static std::equal_to<ForTypem_equal_to;
0
8718
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8499
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
8601
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
7314
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
6162
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
5630
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
4150
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
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
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.