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.
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:
You can either write your own for_each_iter such as:
Or change _init_not_visited() to take GraphType::value_type instead of GraphTypeItr. Assuming that you can still write the function with that argument.