Boost Libraries

Contents

Introduction

Boost is a large collection of C++ libraries that are

Boost is closely connected with the C++ standardisation process:

We take a brief tour of some of the more useful Boost libraries.

Smart Pointers

A smart pointer is a pointer that owns the object it points to and is responsible for deleting it.

A problematic issue with smart pointers is what happens to ownership in copy operation. There are at least the following alternatives:

In Boost there are no deep-copy smart pointers but the other options are available:

Boost also provides:

shared_ptr should be the default smart pointer.

Boost smart pointer documentation and TR1 smart pointer proposal provide more information.

Iterators

Boost has a collection utilities for helping to write standard-conforming iterators:

// fill vector with 1 2 3 4 5
std::vector<int> numbers;
typedef std::vector<int>::iterator n_iter;
std::copy(boost::counting_iterator<int>(1),
          boost::counting_iterator<int>(6),
          std::back_inserter(numbers));

// fill another vector with iterators to the first
std::vector<std::vector<int>::iterator> pointers;
std::copy(boost::make_counting_iterator(numbers.begin()),
          boost::make_counting_iterator(numbers.end()),
          std::back_inserter(pointers));
  • indirect_iterator

    • Iterator to a container of pointers or iterators that behaves as if an iterator to the pointed to objects.
    // print indirect contents
    std::copy(boost::make_indirect_iterator(pointers.begin()),
              boost::make_indirect_iterator(pointers.end()),
              std::ostream_iterator<int>(std::cout, " "));
    // outputs 1 2 3 4 5
    
  • filter_iterator

  • function_output_iterator

  • permutation_iterator

  • transform_iterator

  • reverse_iterator (improved from standard)

  • shared_container_iterator

  • zip_iterator

See Boost Iterator Library documentation for more adaptors and further details.

Functor adaptors

TR1 contains a flexible functor adaptor bind. Boost has two independent implementations of it, boost::bind and boost::lambda.

Here is what bind does:

bind(functor, arg1, arg2, ...)

The bind expressions can grow very complicated. boost::bind overloads some operators to simplify the definition of functors. For example, the above unary functor comparing shoe size and age can also be expressed as

bind(&person::shoe_size, _1) < bind(&person::age, _1);

boost::lambda takes this expression template technique to an extreme.

Regular Expressions

Boost::regex is an implementation of the TR1 regular expressions.

string card_number;
// ...
regex card_number_format ("(\\d{4}[- ]){3}(\\d{4})");
bool is_valid = regex_match (card_number, card_number_format);

There are two basic algorithms:

The string can be given as

There are two optional arguments:

regex_match (string, match_result, expression, flags);

There are also

regex_iterator ri (begin, end, expression);
regex_replace (result, begin, end, expression, format);

More information can be found in boost::regex documentation and TR1 regex proposal

Tuples

TR1 and Boost define a tuple as a generalization of standard pair.

tuple<int, float, string> t(1, 3.5, string("Hello"));

More information in Boost tuple documentation and in TR1 tuple proposal.

Graph Library

Probably the largest library in Boost is the Boost Graph Library (BGL). It provides:

BGL algorithms take the graph type as a template argument. The graph type can be

The graph type Graph has to model graph concepts.

Basic features include:

name_map[u] = "u";
put(name_map, v, "v");
// ...
float & we = weight_map[e];
we = 3.5;
// ...
edge_iterator i, eend;
for (tie(i,eend) = edges(g); i != eend; ++i) {
  cout << "weight("
       << name_map[source(*i,g)] << ',' << name_map[target(*i,g)]
       << ") = " << get(weight_map, *i) << '\n';
}
  • Underlying implementation may vary. For example,
    • vector (descriptor is an integer)
    • member value (descriptor is a pointer)
    • member of vector element
    • boolean property could be a (sign) bit in another value
  • Interior properties are properties of the graph.
property_map<Graph, edge_weight_t>::type weight_map = get(edge_weight, g);
put(weight_map, e, 3.5);
//  is the same as
put(edge_weight_t, g, e, 3.5);
  • Exterior property maps can be created as needed.
  • Property maps are common arguments to algorithms.

Many basic algorithms:

Hash tables

Perhaps the most significant extension to the standard in TR1 are hash table containers.

In Boost, hash tables are available as a part of the Boost multi-index container, which is a set-like container that supports multiple keys and multiple sorting orders.

Boost hash library defines hash functions for many types. They can be used both with TR1-conformant hash tables and with Boost multi-index containers.

Other Boost libraries

Boost contains many more libraries. Here are some highlights: