Boost.Integer defines specialized for integers.
1. types for integers with number of bits
#include <boost/cstdint.hpp> #include <iostream> int main() { boost::int8_t i8 = 1; std::cout << sizeof(i8) << std::endl; #ifndef BOOST_NO_INT64_T boost::uint64_t ui64 = 1; std::cout << sizeof(ui64) << std::endl; #endif boost::int_least8_t il8 = 1; std::cout << sizeof(il8) << std::endl; boost::uint_least32_t uil32 = 1; std::cout << sizeof(uil32) << std::endl; boost::int_fast8_t if8 = 1; std::cout << sizeof(if8) << std::endl; boost::uint_fast16_t uif16 = 1; std::cout << sizeof(uif16) << std::endl; }
output:
1
8
1
4
1
8
types such as boost::int8_t and boost::uint64_t carry the exact memory size in their names. Thus boost::int8_t contains exactly 8 bits, and boost::uint64_t contains exactly 64 bits.
types such as boost::int_least8_t and boost::uint_least32_t contain at least as many bits as their names say. It is possible that the memory size of boost::int_least8_t will be greater than 8 bits and that of boost::uint_least32_t will be greater than 32 bits.
Types such as boost::int_fast8_t
and boost::uint_fast16_t
also have a minimum size. Their actual size is set to a value that guarantees the best performance.
2. more specialized types for integers
#include <boost/cstdint.hpp> #include <iostream> int main() { boost::intmax_t imax = 1; std::cout << sizeof(imax) << std::endl; std::cout << sizeof(UINT8_C(1)) << std::endl; #ifndef BOOST_NO_INT64_T std::cout << sizeof(INT64_C(1)) << std::endl; #endif return 0; }
output
8
4
8
Boost.Integer defines two types, boost::intmax_t
and boost::uintmax_t
, for the maximum width integer types available on a platform.
Boost.Accumulators
An accumulator is a container that calculates a new result every time a value is inserted. The value isn't necessary stored in the accumulator. Instead the accumulator continuously updates intermediary results as it is fed new values.
1. boost::accumulators::tag::count
#include <boost/accumulators/accumulators.hpp> #include <boost/accumulators/statistics.hpp> #include <iostream> using namespace boost::accumulators; int main() { accumulator_set<int, features<tag::count>> acc; acc(4); acc(-6); acc(9); std::cout << count(acc) << std::endl; return 0; }
boost::accumulators::accumulator_set is a template that expects as its first parameter the type of the values that will be processed. The second parameter specifies the accumulators you want to use.
An object of type boost::accumulators::accumulator_set
can be used like a function. Values can be passed by calling operator()
. They are immediately processed. The values passed must have the same type as was passed as the first template parameter to boost::accumulators::accumulator_set.
For every feature, there is an identically named extrator. An extrator receives the current result of an accumulator.
2. using mean and variance
#include <boost/accumulators/accumulators.hpp> #include <boost/accumulators/statistics.hpp> #include <iostream> using namespace boost::accumulators; int main() { accumulator_set<double, features<tag::mean, tag::variance>> acc; acc(8); acc(9); acc(10); acc(11); acc(12); std::cout << mean(acc) << std::endl; std::cout << variance(acc) << std::endl; return 0; }
uses the two features boost::accumulators::tag::mean and boost::accumulators::tag::variance to calculate the mean and the variance of five values. The accumulators selected with boost::accumulators::tag::variance uses weight. If weights are not set explicitly, all values are given the same weight.
3. calculating the weighted variance
#include <boost/accumulators/accumulators.hpp> #include <boost/accumulators/statistics.hpp> #include <iostream> using namespace boost::accumulators; int main() { accumulator_set<double, features<tag::mean, tag::variance>, int> acc; acc(8, weight = 1); acc(9, weight = 1); acc(10, weight = 4); acc(11, weight = 1); acc(12, weight = 1); std::cout << mean(acc) << std::endl; std::cout << variance(acc) << std::endl; return 0; }
Boost.Accumulators uses Boost.Parameter to pass additional parameters, such as weights, as name/value pairs. The parameter name for weights is weight. You can treat the parameter like a variable and assign a value. The name/value pair is passed as an additional parameter after every value to the accumulator.
Boost.MinMax
1. boost::minmax()
#include <boost/algorithm/minmax.hpp> #include <boost/tuple/tuple.hpp> #include <iostream> int main() { int i = 2; int j = 1; boost::tuples::tuple<const int&, const int&> t = boost::minmax(i, j); std::cout << t.get<0>() << std::endl; std::cout << t.get<1>() << std::endl; return 0; }
boost::minmax() returns two values as a tuple. The first reference in the tuple points to the minimum and the second to the maximum.
2. boost::minmax_element()
#include <boost/algorithm/minmax_element.hpp> #include <array> #include <utility> #include <iostream> int main() { typedef std::array<int, 4> array; array a{{2, 3, 0, 1}}; std::pair<array::iterator, array::iterator> p = boost::minmax_element(a.begin(), a.end()); std::cout << *p.first << std::endl; std::cout << *p.second << std:endl; return 0; }
Unlike boost::minmax()
, boost::minmax_element()
returns a std::pair
containing two iterators. The first iterator points to the minimum and the second points to the maximum.
Boost.Random
1.
#include <boost/random.hpp> #include <boost/random/random_device.hpp> #include <iostream> #include <ctime> #include <cstdint> int main() { std::time_t now = std::time(0); boost::random::mt19937 gen{static_cast<std::uint32_t>(now)}; std::cout << gen() << std::endl; boost::random::bernoulli_distribution<> dist1; std::cout << dist1(gen) << std::endl; boost::random::uniform_int_distribution<> dist2{1, 100}; std::cout << dist2(gen) << std::endl; boost::random::random_device gen2; std::cout << gen2() << std::endl; return 0; }
Nearly all of the random number generators provided by Boost.Random are pseudo-random number generators. Pseudo-random number generators don’t generate real random numbers. They are based on algorithms that generate seemingly random numbers. boost::random::mt19937
is one of these pseudo-random number generators.
boost::random::random_device
is a non-deterministic random number generator, which is a random number generator that can generate real random numbers. There is no algorithm that needs to be initialized. Thus, predicting the random numbers is impossible. Non-deterministic random number generators are often used in security-related applications.
Because a coin has only two sides, the random number generator should return 0 or 1. boost::random::bernoulli_distribution
is a distribution that returns one of two possible results.
uses a distribution that is often needed: boost::random::uniform_int_distribution
. This distribution lets you define the range of random numbers you need.
Boost.NumericConversion
1.
#include <boost/numeric/conversion/cast.hpp> #include <iostream> int main() { try { int i = 0x10000; short s = boost::numeric_cast<short>(i); std::cout << s << std::endl; } catch (boost::numeric::bad_numeric_cast &e) { std::cerr << e.what() << std::endl; } return 0; }
boost::numeric_cast does the same conversion as C++, but it verifies whether the conversion can take place without changing the value being converted. Example above of verification fails, and an exception of type boost::numeric::bad_numeric_cast is thrown because 0x10000 is too big to be placed in a variable of type short.