C++/C++11中头文件numeric的使用
<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的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++11中std::initializ
- 下一篇: 深度学习中的欠拟合和过拟合简介