Printing the contents of STL containers in a generic way

A generic Print function A way of using STL algorithms combined with template functions as a means of printing the contents of any type of STL container (eg a std::vector), containing any generic data type (eg int, std::string etc). typename T defines the generic data type held by the container, while typename InputIterator describes the …

Continue reading ‘Printing the contents of STL containers in a generic way’ »

Kruskal’s Algorithm in C++

A simple C++ implementation of Kruskal’s algorithm for finding minimal spanning trees in networks. Though I have a previous posting that accomplishes exactly the same thing, I thought that a simple implementation would be useful, one using a straightforward Graph data structure for modelling network links and nodes, does not have a graphical user interface …

Continue reading ‘Kruskal’s Algorithm in C++’ »

How to convert const_iterators to iterators using std::distance and std::advance

A short posting demonstrating how to remove the const-ness of a const_iterator, given that it is not possible to use re-casting along the lines of const_cast to convert from const_iterator to iterator. Consider the following example: Which results in the following compilation error: error C2440: ‘initializing’ : cannot convert from ‘std::_Vector_const_iterator’ to ‘std::_Vector_iterator’ One approach, …

Continue reading ‘How to convert const_iterators to iterators using std::distance and std::advance’ »