日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

C++/C++11中头文件numeric的使用

發(fā)布時(shí)間:2023/11/27 生活经验 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++/C++11中头文件numeric的使用 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

<numeric>是C++標(biāo)準(zhǔn)程序庫中的一個(gè)頭文件,定義了C++ STL標(biāo)準(zhǔn)中的基礎(chǔ)性的數(shù)值算法(均為函數(shù)模板):

(1)、accumulate: 以init為初值,對迭代器給出的值序列做累加,返回累加結(jié)果值,值類型必須支持”+”算符。它還有一個(gè)重載形式。

(2)、adjacent_difference:對輸入序列,計(jì)算相鄰兩項(xiàng)的差值,寫入到輸出序列中。它還有一個(gè)重載形式。

(3)、inner_product:計(jì)算兩個(gè)輸入序列的內(nèi)積。它還有一個(gè)重載形式。

(4)、partial_sum:計(jì)算輸入序列從開始位置到當(dāng)前位置的累加值,寫入輸出序列中。它還有一個(gè)重載形式。

(5)、iota: 向序列中寫入以val為初值的連續(xù)值序列。

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>測試代碼,詳細(xì)內(nèi)容介紹可以參考對應(yīng)的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

?

總結(jié)

以上是生活随笔為你收集整理的C++/C++11中头文件numeric的使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。