A priority queue in the STL is nothing more than a regular (maximum-) heap. It is essentially an adapter built on top of another container, usually a vector. Therefore, the offered priority queue can easily contain an arbitrary number of elements (so long as memory permits).
But what if we wanted to keep only the biggest N elements, and just have the queue dynamically throw away all other elements? I am sure you can imagine why such a behavior would be much desired: for instance, keeping the best N suggestions for a specific search term springs to mind. Unfortunately, the STL does not support such an adapter.
Continue reading Bounded size priority queue
Tag dispatching is a technique for compile time dispatching between a few overloaded functions by using the properties of a type. This technique usually involves some kind of type traits.
Continue reading Tag dispatching
boost::optional is a very handy generalization of returning a null when there’s nothing to return. Consider a function such as strstr – either the required sub-string is found within the given string, in which case a pointer to it is returned, or a NULL value is returned to indicate that there was no such sub-string. In terms of boost::optional we would either return the same pointer to the occurrence of that sub-string (as before), or we would simply return nothing – no value at all. In this post we will demonstrate the usage of boost::optional and discuss its implementation.
Continue reading boost::optional and its internals
Both std::cin and std::cout provide a conversion to a void* type, but not to a boolean:
operator void* (); // zero if and only if fail()
What use does a conversion to a boolean have, and which pitfalls does the current implementation save us from?
Continue reading Why do cin and cout have conversion to a void* and not bool?
Suppose you have an array of integers and a comparator function object. Your mission, should you choose to accept it, is to sort the array in a reversed order.. Using only a single line of code.
Continue reading One-line rsort of an integer array
Iterating over a vector is a pretty simple task we get to do pretty often. It can be achieved in quite a few ways:
- Using normal random access (operator with index).
- Using std::iterator.
- Using std::for_each algorithm.
I set out to check the runtime differences between all these options, and the results turned out to be a little surprising (or not).
Continue reading Different ways to iterate over a vector
The classes istringstream and 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.
Continue reading Implementing toString() and fromString() using std::stringstream
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.
Continue reading Printing using std::copy