No, that would not work. The parameter for remove_if is a
predicate.
int
is not a predicate.
Approximately:
1 2 3 4 5 6 7 8 9 10
|
template <class Predicate>
void list::remove_if( Predicate pred )
{
auto it = this->begin();
while ( it != this->end() )
{
if ( pred( *it ) ) it = this->erase( it );
else ++it;
}
}
|
Predicate is something that can be called like a function, takes one parameter, and that returns a bool. One has to write a predicate, but then it does not matter how complex data type you have as list::value_type.
Now, you had
(*vIter).GetData() == data
. In a predicate:
1 2 3 4
|
bool equalsData( const Vertices<V, E> & value )
{
return data == value.getData():
}
|
But that naturally fails in lack of data. Lets try
functor (function object):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
class equalsData
{
const V & data_;
public:
equalsData( const V & value ) : data_( value ) {}
bool operator() ( const Vertices<V, E> & value ) const
{
return data_ == value.getData():
}
};
void Graph<V, E>::DeleteVertice( V data )
{
m_graph.remove_if( equalsData( data ) );
}
|
Now we create a nameless temporary
class equalsData
object, initialize its data_ member, and then pass a copy to the
list::remove_if
. remove_if calls the operator() of equalData with each item in the list. If an item matches the data, it will be removed.
You have to fill in the template stuff syntax yourself.
If your compiler supports enough C++11, then you can replace the separate predicate with a lambda expression. I probably should have used it in the example, but haven't been sufficiently fortunate yet.