Reputation: 13267
How to call class member function using std::for_each
Here is what my class is.
template <typename _TyG>
class Graph {
private:
public:
typedef typename Vertex<_TyG> VertexType;
typedef typename std::set<VertexType> GraphStorage;
typedef typename GraphStorage::iterator GraphStorageItr;
typedef typename GraphStorage::const_iterator GraphStorageConstItr;
typedef typename std::list<VertexType *> AdjListType;
typedef typename AdjListType::iterator AdjListTypeItr;
typedef typename AdjListType::const_iterator AdjListTypeConstItr;
typedef typename std::map< VertexType*,
AdjListType,
CompareIterator<VertexType*> > GraphType;
typedef typename GraphType::iterator GraphTypeItr;
typedef typename GraphType::const_iterator GraphTypeConstItr;
void _init_not_visited(GraphTypeItr inItr) {
}
void DepthFirstSearch() {
std::for_each(m_Graph.begin(), m_Graph.end(), std::bind2nd(std::mem_fun(&Graph::_init_not_visited),this));
}
}
What I want is to pass iterator to _init_not_visited
. But I am confuse on how to use std::for_each
, current code is giving me compilation error.
Upvotes: 0
Views: 228
Reputation: 2996
I do not think you can do what you want to do with std::for_each. As std::for_each calls the given function on each value in the sequence and not each iterator position in the sequence. std::for_each might look like:
template <class iter_t, class fn_t>
fn_t for_each(iter_t first iter_t const last, fn_t fn)
{
for (; first != last; ++first)
fn(*first);
return fn;
}
You can either write your own for_each_iter such as:
template <class iter_t, class fn_t>
fn_t for_each_iter(iter_t first, iter_t const last, fn_t fn)
{
for (; first != last; ++first) {
fn(first);
}
return fn;
}
Or change _init_not_visited() to take GraphType::value_type instead of GraphTypeItr. Assuming that you can still write the function with that argument.
Upvotes: 2