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_function<vector<int>,int,void>
{
void
operator() (vector<int> & cardsAvailable, int cardToRemove )const
{
vector<int>::iterator iter;
iter = lower_bound(cardsAvailable.begin(),cardsAvailable. 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_back(10);
ourCards.push_back(20);
vector<int> cardsAvailable;
vector<int>::iterator 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(removeCard(),cardsAvailable) ); // 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\include\functional(279) :
error C2664: 'void removeCard:

perator ()(std::vector<_Ty> &,int)
const' :
cannot convert parameter 1 from 'const
std::binary_function<_Arg1,_Arg2,
_Result>::first_argument_type' to 'std::vector<_Ty> &'
with
[
_Ty=int
]
and
[
_Arg1=std::vector<int>,
_Arg2=int,
_Result=void
]
and
[
_Ty=int
]
Conversion loses qualifiers
C:\Program Files\Microsoft Visual Studio .NET
2003\Vc7\include\functional(27

:
while compiling class-template member function
'std::binder1st<_Fn2>::result_type
std::binder1st<_Fn2>:

perator ()(std::binder1st<_Fn2>::argument_type
&) const'
with
[
_Fn2=removeCard
]
RemoveCard.cpp(52) : see reference to class template instantiation
'std::binder1st<_Fn2>' being compiled
with
[
_Fn2=removeCard
]
*/