Posts

Showing posts from July, 2012

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 STL container iterators passed to it:

Kruskal's Algorithm in C++

Image
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 and does not use the Boost Graph Library, which can be complicated to use and not to everyone's preference.

How to sort items contained in STL maps

By definition you cannot sort a std::map by value, since a std::map sorts its elements by key. This posting documents a way to get around this by dumping std::map key-value pairs into a std::vector first, then sorting that std::vector with a less-than functor afterwards

Modeling networks as graphs in C++

After playing around with some graph algorithm code as implemented by Sedgewick and others, I have come to the conclusion that while these are efficient and concise, improvements could be made to their usability.

Finding substrings within strings using the Boyer-Moore-Horspool Algorithm in C++

Ever wondered what computer algorithm gets employed when using Ctrl-F to search for specific words in a page of text?

Creating sequence diagrams in Enterprise Architect

Image
A few screenshots describing how to create sequence diagrams using Enterprise Architect. There is an excellent YouTube presentation, which is where I got a lot of this information from, available from here: http://www.youtube.com/watch?v=ehz3ha5Jp94

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

Image
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: [code language="cpp"] #include <iostream> #include <vector> class A { public: A( int x ) { val = x; } void DoStuff() { std::cout << "val = " << val << std::endl; } private: int val; }; int main() { // Initialise vector with pointers to A objects A* a[] = { new A( 1 ), new A( 2 ), new A( 3 ), new A( 4 ) }; std::vector<A*> v( a, a + 4 ); // Iterate over the elements for ( std::vector<A*>::const_iterator cit = v.begin(); cit != v.end(); ++cit ) { std::vector<A*>::iterator it = cit; } return 0; } [/code] Which results in the following compilation error: error C2440: 'initializing' : cannot convert from 'std::_...