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!
delete and free() in C++ In C++, the delete operator should only be used either for the pointers pointing to the memory allocated using new operator or for a NULL pointer, and free() should only be used either for the pointers pointing to the memory allocated using malloc() or for a NULL pointer. It is an operator.
Yes. vector::erase destroys the removed object, which involves calling its destructor.
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));
// ^^^^^^^^^^^^^^^^^
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With