<numeric>是C++标准程序库中的一个头文件,定义了C++ STL标准中的基础性的数值算法(均为函数模板):

(1)、accumulate: 以init为初值,对迭代器给出的值序列做累加,返回累加结果值,值类型必须支持”+”算符。它还有一个重载形式。

(2)、adjacent_difference:对输入序列,计算相邻两项的差值,写入到输出序列中。它还有一个重载形式。

(3)、inner_product:计算两个输入序列的内积。它还有一个重载形式。

(4)、partial_sum:计算输入序列从开始位置到当前位置的累加值,写入输出序列中。它还有一个重载形式。

(5)、iota: 向序列中写入以val为初值的连续值序列。

The algorithms are similar to the Standard Template Library (STL) algorithms and are fully compatible with the STL but are part of the C++ Standard Library rather than the STL. Like the STL algorithms, they are generic because they can operate on a variety of data structures. The data structures that they can operate on include STL container classes, such as vector and list, and program-defined data structures and arrays of elements that satisfy the requirements of a particular algorithm. The algorithms achieve this level of generality by accessing and traversing the elements of a container indirectly through iterators. The algorithms process iterator ranges that are typically specified by their beginning or ending positions. The ranges referred to must be valid in the sense that all pointers in the ranges must be dereferenceable and within the sequences of each range, the last position must be reachable from the first by means of incrementation.

下面是从其它文章中copy的<numeric>测试代码,详细内容介绍可以参考对应的reference:

#include "numeric.hpp"
#include <iostream>
#include <functional>
#include <numeric>// reference: http://www.cplusplus.com/reference/numeric/namespace numeric_ {/*
std::accumulate: The behavior of this function template is equivalent to :
template <class InputIterator, class T>
T accumulate (InputIterator first, InputIterator last, T init)
{while (first!=last) {init = init + *first;  // or: init=binary_op(init,*first) for the binary_op version++first;}return init;
}
*/static int myfunction(int x, int y) { return x + 2 * y; }struct myclass {int operator()(int x, int y) { return x + 3 * y; }
} myobject;int test_numeric_accumulate()
{int init = 100;int numbers[] = { 10, 20, 30 };std::cout << "using default accumulate: ";std::cout << std::accumulate(numbers, numbers + 3, init); // 160std::cout << '\n';std::cout << "using functional's minus: ";std::cout << std::accumulate(numbers, numbers + 3, init, std::minus<int>()); // 40 std::minus => x - ystd::cout << '\n';std::cout << "using custom function: ";std::cout << std::accumulate(numbers, numbers + 3, init, myfunction); // 220std::cout << '\n';std::cout << "using custom object: ";std::cout << std::accumulate(numbers, numbers + 3, init, myobject); // 280std::cout << '\n';return 0;
}///
/*
std::adjacent_difference: The behavior of this function template is equivalent to
template <class InputIterator, class OutputIterator>
OutputIterator adjacent_difference (InputIterator first, InputIterator last, OutputIterator result)
{if (first!=last) {typename iterator_traits<InputIterator>::value_type val,prev;*result = prev = *first;while (++first!=last) {val = *first;*++result = val - prev;  // or: *++result = binary_op(val,prev)prev = val;}++result;}return result;
}
*/static int myop(int x, int y) { return x + y; }int test_numeric_adjacent_difference()
{int val[] = { 1, 2, 3, 5, 9, 11, 12 };int result[7];std::adjacent_difference(val, val + 7, result);std::cout << "using default adjacent_difference: ";for (int i = 0; i<7; i++) std::cout << result[i] << ' '; // 1 1 1 2 4 2 1std::cout << '\n';std::adjacent_difference(val, val + 7, result, std::multiplies<int>()); // x * ystd::cout << "using functional operation multiplies: ";for (int i = 0; i<7; i++) std::cout << result[i] << ' '; // 1 2 6 15 45 99 132std::cout << '\n';std::adjacent_difference(val, val + 7, result, myop); // 1 3 5 8 14 20 23std::cout << "using custom function: ";for (int i = 0; i<7; i++) std::cout << result[i] << ' ';std::cout << '\n';return 0;
}/*
std::inner_product: The behavior of this function template is equivalent to:
template <class InputIterator1, class InputIterator2, class T>
T inner_product (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, T init)
{while (first1!=last1) {init = init + (*first1)*(*first2);// or: init = binary_op1 (init, binary_op2(*first1,*first2));++first1; ++first2;}return init;
}
*/static int myaccumulator(int x, int y) { return x - y; }
static int myproduct(int x, int y) { return x + y; }int test_numeric_inner_product()
{int init = 100;int series1[] = { 10, 20, 30 };int series2[] = { 1, 2, 3 };std::cout << "using default inner_product: ";std::cout << std::inner_product(series1, series1 + 3, series2, init); // 240std::cout << '\n';std::cout << "using functional operations: ";std::cout << std::inner_product(series1, series1 + 3, series2, init, std::minus<int>(), std::divides<int>()); // 70std::cout << '\n';std::cout << "using custom functions: ";std::cout << std::inner_product(series1, series1 + 3, series2, init,myaccumulator, myproduct); // 34std::cout << '\n';return 0;
}//
/*
std::partial_sum: The behavior of this function template is equivalent to:
template <class InputIterator, class OutputIterator>
OutputIterator partial_sum (InputIterator first, InputIterator last, OutputIterator result)
{if (first!=last) {typename iterator_traits<InputIterator>::value_type val = *first;*result = val;while (++first!=last) {val = val + *first;   // or: val = binary_op(val,*first)*++result = val;}++result;}return result;
}
*/static int myop_(int x, int y) { return x + y + 1; }int test_numeric_partial_sum()
{int val[] = { 1, 2, 3, 4, 5 };int result[5];std::partial_sum(val, val + 5, result);std::cout << "using default partial_sum: ";for (int i = 0; i<5; i++) std::cout << result[i] << ' '; // 1 3 6 10 15std::cout << '\n';std::partial_sum(val, val + 5, result, std::multiplies<int>());std::cout << "using functional operation multiplies: ";for (int i = 0; i<5; i++) std::cout << result[i] << ' '; // 1 2 6 24 120std::cout << '\n';std::partial_sum(val, val + 5, result, myop_);std::cout << "using custom function: ";for (int i = 0; i<5; i++) std::cout << result[i] << ' '; // 1 4 8 13 19std::cout << '\n';return 0;
}///
/*
std::iota: The behavior of this function template is equivalent to:
template <class ForwardIterator, class T>
void iota (ForwardIterator first, ForwardIterator last, T val)
{while (first!=last) {*first = val;++first;++val;}
}
*/int test_numeric_iota()
{int numbers[10];std::iota(numbers, numbers + 10, 100);std::cout << "numbers:";for (int& i : numbers) std::cout << ' ' << i; // 100 101 102 103 104 105 106 107 108 109std::cout << '\n';return 0;
}} // namespace numeric_

GitHub: https://github.com/fengbingchun/Messy_Test

C++/C++11中头文件numeric的使用相关推荐

  1. C++11中头文件type_traits介绍

    C++11中的头文件type_traits定义了一系列模板类,在编译期获得某一参数.某一变量.某一个类等等类型信息,主要做静态检查. 此头文件包含三部分: (1).Helper类:帮助创建编译时常量的 ...

  2. C++/C++11中头文件functional的使用

    <functional>是C++标准库中的一个头文件,定义了C++标准中多个用于表示函数对象(function object)的类模板,包括算法操作.比较操作.逻辑操作:以及用于绑定函数对 ...

  3. C++11中头文件atomic的使用

    原子库为细粒度的原子操作提供组件,允许无锁并发编程.涉及同一对象的每个原子操作,相对于任何其他原子操作是不可分的.原子对象不具有数据竞争(data race).原子类型对象的主要特点就是从不同线程访问 ...

  4. C++11中头文件thread的使用

    C++11中加入了<thread>头文件,此头文件主要声明了std::thread线程类.C++11的标准类std::thread对线程进行了封装.std::thread代表了一个线程对象 ...

  5. C++11中头文件chrono的使用

    在C++11中,<chrono>是标准模板库中与时间有关的头文件.该头文件中所有函数与类模板均定义在std::chrono命名空间中. std::chrono是在C++11中引入的,是一个 ...

  6. C++/C++11中头文件sstream介绍

    C++使用标准库类来处理面向流的输入和输出:(1).iostream处理控制台IO:(2).fstream处理命名文件IO:(3).stringstream完成内存string的IO. 类fstrea ...

  7. C++11中头文件ratio的使用

    include<ratio>是在C++11中引入的,在此文件中有一些模板类. 模板类std::ratio及相关的模板类(如std::ratio_add)提供编译时有理数算术支持.此模板的每 ...

  8. C++/C++11中头文件algorithm的使用

    <algorithm>是C++标准程序库中的一个头文件,定义了C++ STL标准中的基础性的算法(均为函数模板).<algorithm>定义了设计用于元素范围的函数集合.任何对 ...

  9. C++/C++11中头文件iterator的使用

    <iterator>是C++标准程序库中的一个头文件,定义了C++ STL标准中的一些迭代器模板类,这些类都是以std::iterator为基类派生出来的.迭代器提供对集合(容器)元素的操 ...

最新文章

  1. 用淘宝购买的win7 U盘系统给苹果笔记本Mac OS安装双系统
  2. php mysql网站导航跳转_PHP微信公众平台跳转网页实现定位思路 By:阿尚
  3. 在python中等号前面与后面分别是什么意思-Python中冒号等于(:=)是什么意思?...
  4. python能做什么游戏好-用Python可以做哪些有意思的小游戏呢?
  5. mysql数据库从删库到跑路之mysql多表查询
  6. AppStore发布产品步骤
  7. 查询在具有最小内存容量的所有PC中具有最快处理器的PC制造商 (20 分)(两种思路+详解)
  8. twitter自定义api_为Twitter4j创建自定义SpringBoot Starter
  9. 11988 - Broken Keyboard (a.k.a. Beiju Text)
  10. sender分析之创建请求
  11. ArrayList源码阅读
  12. synchronized关键字实现同步
  13. 辨异 —— Java 中的抽象类和接口
  14. 如何用OD的跟踪功能分析虚拟机保护
  15. [FAQ03776] [Power]关于RTC唤醒系统问题
  16. DataHub: 现代数据栈的元数据平台的Metadata Ingestion Architecture【元数据摄取架构】讲解
  17. 线下销售增长51%,荣耀靠的可不是把体验店装修的像Apple store
  18. mysql的repeat语句_mysql实例 repeat语句的用法
  19. bp神经网络预测模型原理,BP神经网络预测模型
  20. Java开发主流框架有哪些?

热门文章

  1. 使用Python,OpenCV从图像中删除轮廓
  2. 机器学习(15)精确率召回率F1-score(查看癌症预测结果的精确率、召回率)
  3. php phpqueey内存泄露,phpQuery 占用内存过多的处理方法
  4. Linux下设置mysql和tomcat开机启动
  5. 在rMBP上利用Python的onetimepass库实现Google Authenticator Application的效果
  6. HLS中m3u8格式规范解读
  7. WCDMA系统中的扰码规划
  8. Cmake 交叉编译
  9. Qt 编译一直死循环问题
  10. ubuntu 使用阿里云 apt 源