<numeric>是C++标准程序库中的一个头文件,定义了C++ STL标准中的基础性的数值算法,函数并不多,一共5个,偶有接触,这里一并介绍一下,免得每次网上翻阅查找。
这五个函数分别为:accumulate;adjacent_difference;inner_product;partial_sum;iota。
1. accumulate
功能:以init为初值,对迭代器给出的值序列做累加
,返回累加结果值
- 函数模板
//模板一:默认累加
template <class InputIterator, class T>
T accumulate (InputIterator first, InputIterator last, T init);
//模板二:自定义操作--binary_op
template <class InputIterator, class T, class BinaryOperation>
T accumulate (InputIterator first, InputIterator last, T init,
BinaryOperation binary_op);
- 应用举例
#include <iostream> // std::cout
#include <functional> // std::minus
#include <numeric> // std::accumulate
int myfunction(int x, int y) {
return x + 2 * y; }
struct myclass
{
int operator()(int x, int y) {
return x + 3 * y; }
} myobject;
int main()
{
int init = 100;
int numbers[] = {
10, 20, 30};
std::cout << "using default accumulate: ";
std::cout << std::accumulate(numbers, numbers + 3, init);//init + numbers = 100 + 10 + 20 + 30 = 160
std::cout << '\n';
std::cout << "using functional's minus: ";
std::cout << std::accumulate(numbers, numbers + 3, init, std::minus<int>());//init - numbers = 100 - 10 - 20 - 30 = 40
std::cout << '\n';
std::cout << "using custom function: ";
std::cout << std::accumulate(numbers, numbers + 3, init, myfunction);//init (myfunction) numbers
std::cout << '\n';
std::cout << "using custom object: ";
std::cout << std::accumulate(numbers, numbers + 3, init, myobject);//init (myobject) numbers
std::cout << '\n';
return 0;
}
输出:
using default accumulate: 160
using functional's minus: 40
using custom function: 220
using custom object: 280
2. adjacent_difference
功能:对输入序列,计算相邻两项的差值(后一个减前一个元素),写入到输出序列(result)中
。
- 函数模板
//模板一:默认形式,相邻差值写入至result中
template <class InputIterator, class OutputIterator>
OutputIterator adjacent_difference (InputIterator first, InputIterator last,
OutputIterator result);
//模板二:自定义操作--binary_op
template <class InputIterator, class OutputIterator, class BinaryOperation>
OutputIterator adjacent_difference ( InputIterator first, InputIterator last,
OutputIterator result, BinaryOperation binary_op );
- 应用举例
#include <iostream> // std::cout
#include <functional> // std::multiplies
#include <numeric> // std::adjacent_difference
int myop(int x, int y) {
return x + y; }
int main()
{
int val[] = {
1, 2, 3, 5, 9, 11, 12};
int result[7];
std::adjacent_difference(val, val + 7, result);//后面减前面的:1 1 1 2 4 2 1
std::cout << "using default adjacent_difference: ";
for (int i = 0; i < 7; i++)
std::cout << result[i] << ' ';
std::cout << '\n';
std::adjacent_difference(val, val + 7, result, std::multiplies<int>());//std::multiplies<int>():表示乘法
std::cout << "using functional operation multiplies: ";
for (int i = 0; i < 7; i++)
std::cout << result[i] << ' ';
std::cout << '\n';
std::adjacent_difference(val, val + 7, result, myop);//自定义方法
std::cout << "using custom function: ";
for (int i = 0; i < 7; i++)
std::cout << result[i] << ' ';
std::cout << '\n';
return 0;
}
输出:
using default adjacent_difference: 1 1 1 2 4 2 1
using functional operation multiplies: 1 2 6 15 45 99 132
using custom function: 1 3 5 8 14 20 23
3. inner_product
功能:计算两个输入序列的内积。
- 函数模板
//模板一:默认模板
template <class InputIterator1, class InputIterator2, class T>
T inner_product (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, T init);
//模板二:自定义操作--binary_op1 binary_op2
template <class InputIterator1, class InputIterator2, class T,
class BinaryOperation1, class BinaryOperation2>
T inner_product (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, T init,
BinaryOperation1 binary_op1,
BinaryOperation2 binary_op2);
- 应用举例
#include <iostream> // std::cout
#include <functional> // std::minus, std::divides
#include <numeric> // std::inner_product
int myaccumulator(int x, int y) {
return x - y; }
int myproduct(int x, int y) {
return x + y; }
int main()
{
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);//init = init + (*first1)*(*first2) ==》100 + 10*1 + 20*2 + 30*3
std::cout << '\n';
std::cout << "using functional operations: ";
std::cout << std::inner_product(series1, series1 + 3, series2, init,
std::minus<int>(), std::divides<int>());
std::cout << '\n';
std::cout << "using custom functions: ";
std::cout << std::inner_product(series1, series1 + 3, series2, init,
myaccumulator, myproduct);
std::cout << '\n';
return 0;
}
输出:
using default inner_product: 240
using functional operations: 70
using custom functions: 34
4. partial_sum
功能:计算局部累加和(每次都加上前面的所有元素
),计算结果放入result中。
//模板一:默认计算计算局部累加和
template <class InputIterator, class OutputIterator>
OutputIterator partial_sum (InputIterator first, InputIterator last,
OutputIterator result);
//模板二:自定义操作--binary_op
template <class InputIterator, class OutputIterator, class BinaryOperation>
OutputIterator partial_sum (InputIterator first, InputIterator last,
OutputIterator result, BinaryOperation binary_op);
- 应用举例
#include <iostream> // std::cout
#include <functional> // std::multiplies
#include <numeric> // std::partial_sum
int myop(int x, int y) {
return x + y + 1; }
int main()
{
int val[] = {
1, 2, 3, 4, 5};
int result[5];
std::partial_sum(val, val + 5, result);//每次加入前面所有的元素放入result中
std::cout << "using default partial_sum: ";
for (int i = 0; i < 5; i++)
std::cout << result[i] << ' ';
std::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] << ' ';
std::cout << '\n';
std::partial_sum(val, val + 5, result, myop);//自定义操作myop
std::cout << "using custom function: ";
for (int i = 0; i < 5; i++)
std::cout << result[i] << ' ';
std::cout << '\n';
return 0;
}
输出:
using default partial_sum: 1 3 6 10 15
using functional operation multiplies: 1 2 6 24 120
using custom function: 1 4 8 13 19
5. iota
功能:向序列中写入以val为初值
的连续值序列。
- 函数模板
template <class ForwardIterator, class T>
void iota (ForwardIterator first, ForwardIterator last, T val);
- 应用举例
#include <iostream> // std::cout
#include <numeric> // std::iota
int main()
{
int numbers[10];
std::iota(numbers, numbers + 10, 100);//以100为初值
for (int &i : numbers)
std::cout << ' ' << i;
std::cout << '\n';
return 0;
}
输出:
100 101 102 103 104 105 106 107 108 109
参考:http://www.cplusplus.com/reference/numeric/
今天的文章C/C++标准库之numeric分享到此就结束了,感谢您的阅读。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/11951.html