cplusplus.co.il

Archive for the ‘snippets’ Category

ScopeLogger

Posted by: rmn on: 18/11/2009

The RAII design pattern can be utilized to create simple and intuitive logging facilities, like the one we will present now. Through the useful macro LOG_FUNC, the proposed ScopeLogger will easily create function call graphs at run time, to allow easy debugging and tracking of program execution.

One-line rsort of an integer array

Posted by: rmn on: 03/10/2009

Suppose you have an array of integers and a comparator function object. We would like to have the array sorted in a reverse order.. Using only one line of code.

Portable measurement of execution time

Posted by: rmn on: 19/08/2009

Once in a while so it happens that a programmer wishes to time his own creation. This article presents a simple and portable implementation of a Timer class which saves the time of its creation and computes the time it took until destruction, effectively measuring the runtime spent in the enclosing scope.

The classes istringstream & ostringstream can be utilized to create generic, templated, toString() & fromString() functions. This functionality (which is built-in in many languages, such as Java) may come in very handy, quite often.

Printing using std::copy

Posted by: rmn on: 14/08/2009

From time to time we are required to print out a vector. It is possible to utilize the standard copy algorithm (std::copy) to do just that.


Follow

Get every new post delivered to your Inbox.

Join 25 other followers