Besides my direct code modification , here is an idea:
Instead of the _iterator_for_node member _iterator_for_node , which
- has communication problems
- uselessly closely associated with a specific type of container (causing confusion over the resolution of the template template)
- does nothing but std :: find and throws an exception if not found
I suggest creating the following static (global / spatial) function:
template<class It, class T> It checked_find(It begin, It end, const T& node) { It iter = std::find(begin, end, node); if (iter != end) return iter; throw NoSuchNodeException(); }
It will work with any type of iterator (including non-STL, input stream iterators, forward only, const, reverse iterators ... you name it), and this does not require a clear distinction between const / non const :)
With his help
the working version of your sample code would just read
template<class T> class AdjacencyList { std::vector<T> _nodes; public: void delete_node(const T& node) { _nodes.erase(checked_find(_nodes.begin(), _nodes.end(), node)); } };
Pay attention to the code abbreviation. Always a good sign
Greetings
source share