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


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


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


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


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


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


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


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


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


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


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


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


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 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