JASON
JASON

Reputation: 7491

Using std::remove to delete pointer elements

I'm trying to do

remove(pvec.begin(), pvec.end(), NULL);

to remove NULL pointers in the vector (where pvec is vector<Node*>)

However, the compiler gives:

"ISO C++ forbids comparison between pointer and integer"

How to resolve this? Thanks!

Upvotes: 8

Views: 772

Answers (1)

Andy Prowl
Andy Prowl

Reputation: 126452

In C++11, use nullptr:

remove(pvec.begin(), pvec.end(), nullptr);
//                               ^^^^^^^

Otherwise, perform an explicit cast to a pointer value. If your pvec contains pointers of type foo*, write:

remove(pvec.begin(), pvec.end(), static_cast<foo*>(NULL));
//                               ^^^^^^^^^^^^^^^^^

Upvotes: 16

Related Questions