Introducing the Boost C++ Libraries

What is Boost?

Boost is one of the most highly regarded, expert designed and peer-reviewed set of C++ libraries which implements many generic and specialized algorithms such as maths routines, meta-programming, MPI, state machines, parsers and tokenizers.

Why use Boost?

When programming in C++, boost provides several class templates and helper objects that provide readily available solutions for developers. Some of the benefits of using boost include:
  • Peer reviewed code
  • Efficient implementations of most algorithms
  • Avoids re-writing already existing solutions to problems developers are trying to solve (don't re-invent the wheel)
  • Get started quickly solving the problem related to their domain using efficient code.
  • Unit tested code that has been used in many different production environments.
  • Reduce long-term maintenance costs: the developers trying to debug your code 5 years later don't need to understand how you implemented quicksort - if you've used boost you need not rewrite quicksort and they already understand what the sorting algorithm in the library does.
Boost libraries have long been considered best-in-class implementations. Ten boost libraries have already made their way into the official C++11 standards while others are under consideration for the next iteration of the standards update, i.e. C++17.

A few relevant libraries from a machine learning perspective are listed here:

Installing Boost

Installing under a Linux OS

The Boost API is easily installed under most Linux distributions by installing the relevant packages. For example, run the following commands to install it for Ubuntu/Debian Linux:

sudo apt-get install libboost-all-dev

Installing under Mac OSX

If you're using homebrew, install boost using this command:

brew install boost

If you're using macports, install boost using this command:

sudo port install boost

Installing under Microsoft Windows

Installing Boost in windows is slightly more complicated as it requires building the library from its source code.
Follow these steps to build the Boost libraries with the MinGW64 GCC compiler:
  1. Download the source code archive from - http://www.boost.org/users/download/#live . Its preferred to download the 7z archive since its smaller.
  2. Unpack it into a folder in your machine, for example - c:\boost.
  3. Change to the main folder and run the bootstrap command specifying the compiler to be used - which is gcc in this case: c:\boost\bootstrap.bat gcc
  4. Build it with this b2 command (I've included both static and shared libraries, and multi threading support):
\b2 --prefix=c:\mingw_64\boost64 toolset=gcc address-model=64 variant=debug,release link=static,shared threading=multi  install
This will compile the libraries and store them into the folder c:\mingw_64\boost64 which will contain all the boost headers and libraries.
Next, set these paths in your environment:
  • BOOST_INCLUDEDIR=C:\mingw-w64\boost64\include\boost-1_64;
  • CPATH=C:\mingw-w64\boost64\include\boost-1_64;
  • LIBRARY_PATH=C:\mingw-w64\boost64\lib;
  • LIB=C:\mingw-w64\boost64\lib;
  • PATH=%PATH%;C:\mingw-w64\boost64\lib;
Now you'll be able to compile programs with boost libraries.

An example with boost lambda

The following example program shows how the boost lambda library can be used. Since boost.lambda is a header-only library, you simply need to ensure the library headers are present in the INCLUDE search path or explicitly set in the C++ project's compiler settings.

#include <boost/regex.hpp>
#include <boost/lambda/lambda.hpp>
#include <string>
#include <iostream>
#include <iterator>
#include <algorithm>

using namespace std;

int main(){

    using namespace boost::lambda;
    typedef std::istream_iterator<int> in;

    for_each(
        in(std::cin), in(), cout << (_1 * 3) << " " );

    return(0);
}

In case you've statically linked to a boost library, ensure that your application is re-compiled with updated versions as and when the libraries are updated to resolve bugs and speed up their performance.
Writing good unit-tests and fitness tests helps make the re-compilation process easier.

Continue Learning Boost

The following resources will help you further understand Boost for applying it in your project:
  1. The boost documentation - http://www.boost.org/doc/libs/
  2. Using the C++ Standard and Boost Libraries - https://www.youtube.com/watch?v=zxzyntAwtgc
  3. Beyond the C++ Standard Library: An Introduction to Boost - https://www.amazon.com/dp/0321133544/
  4. A Short Introduction to Selected Classes of the Boost C++ Library - http://quantlib.org/slides/dima-boost-intro.pdf
  5. The Boost C++ Libraries, Boris Schäling - https://theboostcpplibraries.com/

Comments