Re: Copy Constructors and Assignment Operator, When should I use?
On Mar 4, 2:36 am, rockdale <rockdale.gr...@gmail.com> wrote:
You don't need to pass anything. needRemove is a function that takes a
single argument (unary). remove_if is codes as it will call needRemove
for all elements in the range provided. Those individual elements are
what are used to make the call to needRemove and that happens inside
remove_if. You just need to pass the function pointer to the
algorithm. To use remove_if, you should include <algorithm>.
I could not get the remove_if work, Please help me to look the
following code,
I added following 2 methods in class ItemListA (please refer to former
post for the whole code)
void removeEvenItems(){
std::vector<ItemA>::iterat=
or itend =
std::remov=
e_if(m_vecA.begin(), m_vecA.end(),
&ItemListA::needRemove);
m_vecA.erase(itend, m_vecA=
..end());
}
bool needRemove(const ItemA &aItem ){
if((aItem.aInt % 2) ===
0){
return tru=
e;
}else{
return fal=
se;
}
}
I got error C2064: term does not evaluate to a function taking 1
arguments.
The error is as it says. You have passed in a pointer to non-static
member function, that has an implicit this argument alongwith the
const ItemA& one. It is not a unary function. So, either you make it a
static member, better make it a free function or bind the this
argument to it as below:
std::remove_if(m_vecA.begin(), m_vecA.end(),
tr1::bind(&ItemListA::needRemove, this));
Here is a sample compilable code for your reference:
#include <iostream>
#include <vector>
#include <functional>
using namespace std;
struct ItemListA {
struct ItemA {int aInt;};
std::vector<ItemA> m_vecA;
bool needRemove(const ItemA& item) {
if (item.aInt %2)
return true;
else
return false;
}
void removeEven() {
std::remove_if(m_vecA.begin(), m_vecA.end(),
std::tr1::bind(&ItemListA::needRemove, this));
}
};
int main(){
ItemListA itemListA;
itemListA.removeEven();
}