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 = ;
std::cout << sizeof(i8) << std::endl; #ifndef BOOST_NO_INT64_T
boost::uint64_t ui64 = ;
std::cout << sizeof(ui64) << std::endl;
#endif boost::int_least8_t il8 = ;
std::cout << sizeof(il8) << std::endl; boost::uint_least32_t uil32 = ;
std::cout << sizeof(uil32) << std::endl; boost::int_fast8_t if8 = ;
std::cout << sizeof(if8) << std::endl; boost::uint_fast16_t uif16 = ;
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 = ;
std::cout << sizeof(imax) << std::endl; std::cout << sizeof(UINT8_C()) << std::endl; #ifndef BOOST_NO_INT64_T
std::cout << sizeof(INT64_C()) << std::endl;
#endif return ;
}

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();
acc(-);
acc();
std::cout << count(acc) << std::endl;
return ;
}

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();
acc();
acc();
acc();
acc();
std::cout << mean(acc) << std::endl;
std::cout << variance(acc) << std::endl;
return ;
}

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(, weight = );
acc(, weight = );
acc(, weight = );
acc(, weight = );
acc(, weight = );
std::cout << mean(acc) << std::endl;
std::cout << variance(acc) << std::endl;
return ;
}

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 = ;
int j = ; boost::tuples::tuple<const int&, const int&> t = boost::minmax(i, j); std::cout << t.get<>() << std::endl;
std::cout << t.get<>() << std::endl;
return ;
}

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, > array;
array a{{, , , }}; 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 ;
}

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();
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{, };
std::cout << dist2(gen) << std::endl; boost::random::random_device gen2;
std::cout << gen2() << std::endl; return ;
}

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 ;
}

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.

boost number handling的更多相关文章

  1. jqu

    1 /*2 * 说明:3 * 本源代码的中文注释乃Auscarlin呕心沥血所作.旨在促进jQuery的传播以及向广大jQuery爱好者提供一个进阶4 *的途径,以让各位更加深入地了解jQuery,学 ...

  2. 优秀的PHP开源项目集合

    包管理Package Management Package Management Related 框架 框架组件 微框架Micro Frameworks 内容管理系统Content Managemen ...

  3. github上所有大于800 star OC框架

    https://github.com/XCGit/awesome-objc-frameworks#awesome-objc-frameworks awesome-objc-frameworks ID ...

  4. PHP框架、库和软件资源大全(整理篇)

    php的资料 https://github.com/ziadoz/awesome-php Awesome PHP A curated list of amazingly awesome PHP lib ...

  5. Lucene学习总结之八:Lucene的查询语法,JavaCC及QueryParser

    一.Lucene的查询语法 Lucene所支持的查询语法可见http://lucene.apache.org/java/3_0_1/queryparsersyntax.html (1) 语法关键字 + ...

  6. ios7毛玻璃效果实现

    首先看效果:       核心代码: //加模糊效果,image是图片,blur是模糊度 - (UIImage *)blurryImage:(UIImage *)image withBlurLevel ...

  7. C++开源代码项目汇总

    Google的C++开源代码项目 v8  -  V8 JavaScript EngineV8 是 Google 的开源 JavaScript 引擎.V8 采用 C++ 编写,可在谷歌浏览器(来自 Go ...

  8. Squid调试和故障处理

    http://blog.zhdata.com/tag/squid第16章 调试和故障处理 16.1 一些通用问题 在讨论通用debug前,我先提起一些经常发生的问题. 16.1.1 “Failed t ...

  9. Google的C++开源代码项

    转:http://blog.csdn.net/wenrenhua08/article/details/40040903 v8  -  V8 JavaScript EngineV8 是 Google 的 ...

随机推荐

  1. BaseFragment 基类

    package com.test.mvp.mvpdemo.mvp.v6.basemvp; import android.os.Bundle;import android.support.annotat ...

  2. 42 Bing Search Engine Hacks

    42 Bing Search Engine Hacks November 13, 2010 By Ivan Remember Bing, the search engine Microsoft lau ...

  3. 杂项-Unicode:Unicode

    ylbtech-杂项-Unicode:Unicode Unicode(统一码.万国码.单一码)是计算机科学领域里的一项业界标准,包括字符集.编码方案等.Unicode 是为了解决传统的字符编码方案的局 ...

  4. Jenkins使用六:搭建流水线任务

    流水线可以把多个任务串起来,比如发布版本的一系列流程 配置流水线任务 构建语法为Groovy,执行3次test(job名) node { stage("test") { echo ...

  5. 【转载】Spring boot学习记录(二)-配置文件解析

    前言:本系列文章非本人原创,转自:http://tengj.top/2017/04/24/springboot0/ 正文 Spring Boot使用了一个全局的配置文件application.prop ...

  6. node+express解决前端跨域问题

    var express = require('express') , app = express(); //解决跨域 app.all('*',function (req, res, next) { r ...

  7. Unity3D利用Logcat调试安卓

    发布安卓包之后再次测试发生什么问题很难知道怎么了,比如说出现闪退等情况,可以用Logcat检测到,logcat是Android中一个命令行工具,可以用于得到程序的log信息,可以用 logcat 命令 ...

  8. C# 压缩、解压缩

    /// <summary> /// 压缩文件 FNameArry 为客户端传回来的文件列表:文件名数组,压缩包的名称strZipName /// </summary> /// ...

  9. C# 栈=>随时读取栈中最小值

    //原理:利用两个栈,一个记录最小值,一个记录数据. using System; using System.Collections.Generic; using System.Linq; using ...

  10. 任务调度(02)Spring Schedule

    任务调度(02)Spring Schedule [toc] Spring 3.0 提供两种任务调度方式:一是定时任务调度:二是异步任务调度.这两种任务调度方式都是基于 JUC 实现的,是一种非常轻量级 ...