Getting Started with OpenGL in Linux

TweetA brief and hopefully to the point on getting up and running with OpenGL in an Ubuntu Linux environment. For setting up OpenGL in Windows / Visual Studio environments, please see this post. My choice of integrated development environment for … Continue reading


Practical examples of using Boost serialization

TweetListing1: Serialization of STL containers: a std::vector example using text archives Your intuition may tell you to iterate through the STL container in order to serialize it, but it’s actually a lot simpler. First of all, be sure to include … Continue reading


Getting started with SWiG for interfacing between C# and C++ Visual Studio projects

Tweet1. Download and extract the SWiG interface: Windows version available here: http://prdownloads.sourceforge.net/swig/swigwin-2.0.9.zip 2. Create your C# console application 3. Create your C++ console application: Create a new C++ console application inside the existing solution you have open, and call it … Continue reading


Using Win32 static libraries in Visual Studio projects

TweetA static library is simply a file that contains functionality that can be made available to other programs. Static libraries end with the .lib extension and are made available to other Visual Studio projects (console applications, MFC applications etc) by … Continue reading


How to recursively print STL-based trees

TweetThere are plenty of resources on how we may recursively search and print the contents of binary trees. This example shows how to (recursively) make use of the Boost serialization libraries and streams in order to print the contents of … Continue reading


How to link DLLs to C++ Projects

TweetThese instructions, also available on the Microsoft site, show how to create from scratch a Visual Studio 2010 project that can utilize dll routines created elsewhere, by way of referencing. It basically says the same thing, but with additional screenshots … Continue reading


Using stateful functors

TweetFor another example posts on functors (function objects) see here. A functor is an instance of a C++ class that has the operator() defined. One big advantage of functors is that when you define the operator() in C++ classes you … Continue reading


Using boost::bind to assign functions

TweetSome code samples I have collated in the sample below, that demonstrate how boost::function can be assigned with functors, ordinary functions, class member functions and overloaded class member functions respectively. #include <iostream> #include <boost/function.hpp> #include <boost/bind.hpp> using namespace std; // … Continue reading


Getting started with the Boost libraries in Cygwin

TweetThis post assumes that the boost libraries have been downloaded and extracted to the directory of your choice. See this previous posting for more details on how to download and extract the Boost libraries. Open Cygwin and cd to the … Continue reading


Using boost::bind as an improved means of calling member functions

TweetThis post takes a look at using boost::bind as a means of calling class member functions in an efficient and generic way. It basically summarizes what has already been said at Björn Karlsson’s excellent Informit article. Since I found the … Continue reading


Getting started with the Boost libraries in Ubuntu Linux

Tweet1. Install the Boost libraries from the command line First try the following $ sudo apt-get install libboost* You may get an error message similar to the following, like I did: E: Unable to correct problems, you have held broken … Continue reading


Getting started with Qt in Ubuntu Linux

Tweet1. Obtain QT4 Run these from the command line: $ sudo apt-get update $ sudo apt-get install libqt4-dev qt4-qmake cmake r-base-dev libcurl4-gnutls-dev 2. Create and edit the qmake project file To generate and edit the project file itself use something … Continue reading


Factory Patterns in C++

TweetIn a nutshell, the factory design pattern enables the practitioner to create objects that share a common theme but without having to specify their exact classes. In essence the factory pattern is used to “Define an interface for creating an … Continue reading


How to interface with Excel in C++

TweetInterfacing Excel in C++ is task that I needed to overcome recently, so I thought I would post some code and instructions on the said topic. Some online articles that I found to be useful include the following: A Brief … Continue reading


STL Set Operations

TweetA summary with code samples of the set operations that come with STL: union, intersection, symmetrical difference and set difference. For consistency, the two sets of integer vectors used in each example are the same and are: vals1 = { … Continue reading


Getting started with Windows GDI graphics applications in C++

TweetA guide to getting started with Windows graphics applications for the very first time. The Windows Graphics Device Interface (GDI) forms the basis of drawing lines and objects, and from this device contexts. I will not go into any detail … Continue reading


Templates in C++

TweetAn introduction to using template classes in C++, that starts extremely simply and builds up from there… Example 1 Consider the following simple class that we use to store and print an integer: #include <iostream> class A { private: int … Continue reading


Printing the contents of STL containers in a generic way

TweetA 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 … Continue reading


Kruskal’s Algorithm in C++

TweetA 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 … Continue reading


How to sort items contained in STL maps

TweetBy 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 … Continue reading


Modeling networks as graphs in C++

TweetAfter 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. After studying Sedgewick’s books and online … Continue reading


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

TweetEver wondered what computer algorithm gets employed when using Ctrl-F to search for specific words in a page of text? A number of potential string searching techniques exist. One possible candidate implementation is Nigel Horspool’s Boyer-Moore-Horspol algorithm – an exceptionally … Continue reading


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

TweetA 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: #include <iostream> #include <vector> … Continue reading


Creating smart pointers in C++

TweetIntroduction What are smart pointers? They are a means of handling the problems associated with normal pointers, namely memory management issues like memory leaks, double-deletions, dangling pointers etc. This post gives a simple guide to creating your own smart pointer … Continue reading


The K-Shortest Paths Algorithm in C++

TweetIntroduction Following on from a previous post which was concerned with finding all possible combinations of paths between communicating end nodes, this algorithm finds the top k number of paths: first the shortest path, followed by the second shortest path, … Continue reading


DFS Algorithm Download for Visual Studio 2003

TweetDownload the Visual Studio 2003 project and code. Upon payment you should be promptly directed to a download link. Please contact me if you experience any problems and I will get it sent to you as soon as possible.


Genetic Algorithms Applied to Travelling Salesman Problems in C++

TweetIntroduction Following on from a previous posting on Simulated Annealing applied to travelling salesman problems, here is a posting that carries on in a similar vein, this time focusing on genetic algorithms as our optimization technique of choice. There ia … Continue reading


C++ Implementation of Hill-climbing and Simulated Annealing applied to Travelling Salesman Problems

TweetVisual Studio 2010 project is downloadable from here. Introduction Following from a previous post, I have extended the ability of the program to implement an algorithm based on Simulated Annealing and hill-climbing and applied it to some standard test problems. … Continue reading


C++ Implementation of 2-opt to the “Att48″ Travelling Salesman Problem

TweetIntroduction Some initial results from experimenting with 2-opt and 3-opt heuristics and applying them to a standard travelling salesman test problem. Visual Studio 2010 project given here. A nearest neighbour search algorithm is included. A comparison is made of the … Continue reading


Number System Conversions in C++

TweetA posting which I will probably update from time to time that summarizes the conversion functions I encounter in C++. Hope others will find this useful too. 32-bit IEEE 754 floating point value to binary string std::string GetBinary32( float value … Continue reading


A Genetic Algorithm Function Optimizer in C++

TweetIntroduction An example is presented here on how an algorithm employing standard genetic operators can be applied to optimize a standard mathematical function, such as the Rosenbrock function: Sample Visual Studio projects implementing this genetic algorithm are available here and … Continue reading


Converting between binary and decimal representations of IEEE 754 floating-point numbers in C++, Java and Python

TweetThis post implements a previous post that explains how to convert 32-bit floating point numbers to binary numbers in the IEEE 754 format. What we have is some C++ / Java / Python routines that will allows us to convert … Continue reading


Mixing Managed and Native Types in C++ / CLI

TweetBrief Introduction C++/CLI (Common Language Infrastructure) was designed to bring C++ to .NET as a means of developing managed code applications. Specifically it helps simplify writing managed code when using C++. Suppose you are writing a WinForms application in Microsoft … Continue reading


Mapping Words to Line Numbers in Text Files in STL / C++

TweetFollowing on from the previous post, this example shows an example of how to use an STL multimap to track the line number(s) associated with each word in a text file. This program essentially reads in text line-by-line, while stripping … Continue reading


Counting the Number of Words in a Text File in STL / C++

TweetThis post aims to illustrate the power of using STL’s associative arrays as a word counter. It reads the entire contents of the text file, word-by-word, and keeps a running total of the number of occurences of each word. All … Continue reading


Mathematical Expression Parsers in Java and C++

TweetBasic Expression Parsing Click here for advanced expression parsing When writing your own calculator it is necessary to build a converter that can transform an input mathematical expression such as ( 1 + 8 ) – ( ( 3 * … Continue reading


Implementing Dijkstra’s Algorithm using Sedgewick’s C++ Code

TweetIntroduction Dijkstra’s algorithm solves the shortest path problem for a graph with nonnegative edge weights, producing a shortest path tree. This algorithm is often used in routing and as a subroutine in other graph algorithms, the k-shortest paths algorithm, for … Continue reading


Priority Queues and Min Priority Queues in STL / C++

TweetPriority Queues A priority queue is just like a normal queue data structure except that each element inserted is associated with a ”priority”. It supports the usual push(), pop(), top() etc operations, but is specifically designed so that its first … Continue reading


Static Classes and Static Class Members in C#

TweetStatic classes – what are they? In C#, static classes have one important difference to that of non-static classes: they cannot be instantiated. That is, the new operator cannot be used to create an instance of the class type. Since … Continue reading


How to Permanently Remove Items in STL Containers

Tweetremove – What is does and does not do Like all STL algorithms, remove receives a pair of iterators to identify the range of container elements over which it needs to operate, as shown in its declaration: template< class ForwardIterator, … Continue reading


A Simple Binary Tree Implementation in C++

TweetA very basic binary tree implementation in C++ that defines a binary tree node, adds new nodes and prints the tree. #include <stdio.h> class Node { public: Node( int v ) { data = v; left = 0; right = … Continue reading


Hash Tables as a means of fast lookup in STL / C++.

TweetIntroduction In a previous life I was involved in the design of routing optimization software for the telecoms industry. Finding the least cost route for a traffic demand between communicating network sites necessitates a search for all the tariffs provided … Continue reading


A Recursive Algorithm to Find all Paths Between Two Given Nodes

TweetProblem Outline This I tackled previously when working on the design and implementation of routing optimization algorithms for telecommunications networks. Given that a wide area network with nodes and interconnecting links can be modelled as a graph with vertices and … Continue reading


Implementing Kruskal’s Algorithm in C#

TweetThis post is essentially a blatant lifting of Omar Gamil’s CodeProject article on the same subject. I have been using the project as means of getting into C# programming and using things like Windows Forms etc in Visual Studio environments … Continue reading


Getting Started with C# WPF Applications

TweetSome notes on how to create a simple Windows Presentation Foundation (WPF) application and get familiar with the Visual C# integrated development environment (IDE). Like Windows Forms applications, WPF applications can be developed by dragging controls from the Toolbox to … Continue reading


The Big Three in C++

TweetConstructor, destructors and assignment operators There is a rule of thumb in C++ that if a class defines a destructor, constructor and copy assignment operator – then it should explicitly define these and not rely on their default implementation. Why … Continue reading


A First Stab at boost::bind

TweetBoost::bind is “able to bind any argument to a specific value or route input arguments into arbitrary positions.” It’s a means of converting a function into an object that can be copied around and called at a later point, deferred … Continue reading


Avoiding Memory Leaks using Boost Libraries

TweetUsing boost::scoped_array When we want to dynamically allocate an array of objects for some purpose, the C++ programming language offers us the new and delete operators that are intended to replace the traditional malloc() and free() subroutines that are part … Continue reading


Dynamically Created MFC Controls and their Notifications

TweetI was required to create a dialog application with dynamically created checkboxes (CButtons).  Given that selection of a certain hardware types in the software would result in completely different checkbox layouts.  In these situations, it is the software that has … Continue reading


Getting Started with Boost Threads in Visual Studio

TweetIntroduction This post aims to be an accessible introduction to getting set up with the Boost threads in Visual Studio environments for the first time.  Like with many technical subjects, there is a great deal of information out on the … Continue reading


Using Template Classes to Handle Generic Data Types in STL Containers

TweetA code snippet with which utilizes both template classes and STL to handle generic data types. In keeping with the spirit of this blog, I have kept the explanation to a minimum and hopefully the code posted below should be … Continue reading


Getting Started with the CU Decision Diagram (CUDD) Package for Windows

TweetI have managed to download, uncompress and compile this library in a Visual Studio environment. At last.  Though a very powerful package for the implementation of binary decision diagrams, the documentation for its actual set-up for non-Unix environments seems a … Continue reading


Getting Started with UnitTest++ in Visual Studio

TweetFor some time I wanted to incorporate unit testing into our development process. Though many think unit testing is largely a waste of time with not all code bases lending themselves easily to testing, if done in the right spirit … Continue reading


Simple Multithreading in C++ / MFC

TweetRight now I am just starting with a very simple example – no fancy stuff – just a single thread to demonstrate a sequence that updates a counter 20 times per second. //—- First example: Using global vars and fn … Continue reading


Some neat examples of C++ String Formatting

TweetSome neat string formatting A pretty neat snippet found on stackoverflow recently (kudos David Rodriguez), that is worth repeating. I’m gonna start using this. Essentially it is a bit of in-house stream formatting. This class make_string produces an instance of … Continue reading


Sorting Objects Using STL

TweetHere’s an example of how using objects (hat-tip: Paul Wolfensberger) #include <iostream> #include <ostream> #include <vector> #include <algorithm> // A base class to hold, return and output a value class Item { private: int val; public: Item( int x ) … Continue reading


Using Smart Pointers to Avoid Memory Leaks

TweetUsing boost::scoped_array When we want to dynamically allocate an array of objects for some purpose, the C++ programming language offers us the new and delete operators that are intended to replace the traditional malloc() and free() subroutines that are part … Continue reading


Using Function Objects (Functors) in STL / C++

TweetGenerically, function objects (or functors) are class instances whose member function operator() has been defined. This member function allows the object to be used with the same syntax as a regular function call, and therefore its type can be used … Continue reading


User Defined Predicates

TweetJust some simple examples posted here as a means of easy lookup… class MyValue { public: MyValue::MyValue( int v ) : value( v ) {} int value; }; class MyValue_eq : public unary_function< MyValue, bool > { int value; public: … Continue reading


‘Blitting’ a Bitmap Image to the Screen.

TweetI recently needed to revisit this to create simple monochrome bitmaps representing the sets of nozzles turned off/on on a Xaar microarrayer printhead. Sample code here.  Essential steps are outlined in the following code // 1. Create the uninitialized bitmap, … Continue reading


Multidimensional Arrays in C++

TweetDeclaring and initializing multi-dimensional arrays in C++ can be done not just by way of traditional pointer arithmetic, but using the STL / Boost libraries as well.  Here are some examples: 2D arrays using std::vector #include <iostream> #include <vector> using … Continue reading


A Genetic Algorithm Based Routing Optimization Tool

Tweet(For source code see this updated post.) Introduction This post describes the use of a tool written in C++ that could be used to assist a network designer in establishing an optimal set of virtual paths in ATM networks. In … Continue reading


Maintaining Recent File Lists in MFC Applications

TweetA simple application in Visual Studio 2003 that builds on the “MyPad” application provided by Jeff Prosise in Programming Windows with MFC. Alternatively you could probably create the same by running the AppWizard to create a standard view-based application, but … Continue reading


How to Create Resizable Dialogs in MFC

TweetExample 1: Using Paulo Messina’s Code Project sample A minimalist use of the CResizeableDialog class as described by Paulo Messina at CodeProject .   Hope you find the following recipe useful. Visual Studio 2003 source code is downloadable from here. 1. … Continue reading


Finding Minimal Spanning Trees using Kruskal’s Algorithm in MFC / C++ / Boost libraries

TweetKruskal’s algorithm is used to find the minimal spanning tree for a network with a set of weighted links. This might be a telecoms network, or the layout for planning pipes and cables, or one of many other applications. It … Continue reading


Avoiding Circular Dependencies using Observer Patterns in C++

Tweet The basic concept is when you want information stored in one object, called the Model (or subject) to be watched by other objects, called Views (or observers). When information changes in the Model, all views attached to the Model … Continue reading