Concept, Installation and Examples of using the Boost library

⏱️ Boost is Multiplatform: Windows, macOS, GNU/Linux, BSD and any other operating system and architecture.


Concept, Installation and Examples of using the Boost library


Boost is a set of libraries for the C++ programming language that provides support for tasks and structures such as linear algebra, pseudorandom number generation, multithreading, image processing, regular expressions and unit testing.

It contains 164 individual libraries (as of version 1.76).

All Boost libraries are licensed under the Boost Software License, designed to allow Boost to be used with both open and proprietary software projects.

Many of Boost’s founders are on the C++ standards committee (so the Boost website is often seen as the C++ website), and several libraries created by Boost have been accepted for incorporation into C++ (Technical Report 1), such as:

The Boost community emerged around 1998, when the first version of the standard was released.

The original founders of Boost are still active in the community, including Nicolai Josuttis, a writer of books from C++ that I I have the pleasure of owning one of his books: 📘

Book by Nicolai Josuttis


Installation

On Windows

It is interesting to use package managers such as: Winget, however, the best option is to use vcpkg

Using vcpkg

vcpkg is a popular package manager for C++ that makes it easy to install libraries, including Boost.

  • Download and install vcpkg: Open PowerShell and run the following commands to download and configure vcpkg:
git clone https://github.com/microsoft/vcpkg.git
cd vcpkg
.\bootstrap-vcpkg.bat

Install Boost using vcpkg, still in PowerShell, run the command to install Boost:

.\vcpkg install boost

Integrate vcpkg with your development environment. To use the libraries installed by vcpkg, you need to integrate vcpkg with your development environment. For Visual Studio, run:

.\vcpkg integrate install

This allows Visual Studio to automatically detect libraries installed by vcpkg.

Another way to install Boost on Windows is to use the official installer available on the Boost website:

Extract the contents of the .zip or .7z file to a directory of your choice.

Compile Boost (optional): Some Boost modules may need to be compiled. To compile, open Command Prompt and navigate to the Boost directory. Then run:

bootstrap.bat
.\b2

This will compile the necessary modules and create the library files.

Configure the Boost path in your development environment (such as Visual Studio) so that it can find the header files and compiled libraries.


In Ubuntu:

To install the Boost library on Ubuntu via APT, run these commands on your terminal:

sudo apt update
sudo apt install libboost-all-dev

If you want to install only specific modules. For example, to install just Boost.Filesystem and Boost.System, you can use: sudo apt install libboost-filesystem-dev libboost-system-dev.

After installing the Boost library, you can verify the installation by checking the installed version:

dpkg -s libboost-all-dev | grep 'Version'

Now let’s see how to use Boost with some examples!


01. Clean up whitespace at the beginning and end of a string

You know that ready-made PHP function that you wish existed in STL? Yes, the one that removes white spaces, often used to process input from HTML?

At Boost, you just include the header: string.hpp and use it, for example:

#include <iostream>
#include <boost/algorithm/string.hpp>

int main() {
 std::string str = " Hello, World! ";
 boost::algorithm::trim(str);
 std::cout << "'" << str << "'" << std::endl;
 return 0;
}

Of course you can do this feat from scratch with algorithm or regex, but there’s nothing like having it ready just to use.


02. Converts a string to a specific type or vice versa, safely and conveniently.

And those conversions that sometimes annoy, in Boost just include #include <boost/lexical_cast.hpp> and use:

int i = boost::lexical_cast<int>("123");
std::string s = boost::lexical_cast<std::string>(456);

As not all types are changeable, the ideal is to use it within a try catch!


03. Replaces all occurrences of one substring with another.

Once again, the people at web development, love using this functionality. And this in Boost just includes the #include <boost/algorithm/string.hpp>:

std::string s = "Hello World";
boost::algorithm::replace_all(s, "World", "Boost");
// s is now "Hello Boost"

04. Creates functions and functors through argument binding.

If you still don’t understand why bind arguments, I suggest reading this article.

auto f = boost::bind(std::plus<int>(), 1, 2);
int result = f(); // result is 3

These functions and classes are just a small sample of the vast functionality that the Boost library offers, making it a powerful and essential tool for C++ developers.

Several software use Boost, including: Asio, Dlang, POCO, Red Programming Language and many others!


The official Boost website address is: https://www.boost.org and the official Boost repository on GitHub can be found at the following address: https://github.com/boostorg/boost. And more useful links: https://boost-spirit.com/home/ and https://en.wikipedia.org/wiki/Boost_(C%2B%2B_libraries).


cpp cppdaily


Share


YouTube channel

Subscribe


Marcos Oliveira

Marcos Oliveira

Software developer
https://github.com/terroo