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
]
*/
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
]
*/
Comment