人网站建站想要给网站加视频怎么做
人网站建站,想要给网站加视频怎么做,西安网站建设 大德,官方网站制作思路1 STL 的数值算法概述
STL 的数值算法提供了一系列用于处理数值计算的模板函数。这些算法主要针对容器中的元素进行数学运算和统计计算#xff0c;使得程序员能够高效地处理数值数据。
STL 数值算法包括了一系列功能丰富的函数#xff0c;例如 std::accumulate 用于计算容器…1 STL 的数值算法概述
STL 的数值算法提供了一系列用于处理数值计算的模板函数。这些算法主要针对容器中的元素进行数学运算和统计计算使得程序员能够高效地处理数值数据。
STL 数值算法包括了一系列功能丰富的函数例如 std::accumulate 用于计算容器中元素的累积值可以方便地进行求和、求积或其他自定义的累积操作。std::inner_product 用于计算两个序列的点积内积这在向量运算中尤为常见。std::partial_sum 则用于计算容器中元素的部分和序列有助于分析序列的局部特性。
此外std::adjacent_difference 可以计算容器中每对相邻元素的差这在分析序列的变化趋势时非常有用。而 std::iota 则可以将一系列连续的值填充到容器中用于生成序列或初始化容器。
这些数值算法的设计目标是提供一种高效、可靠且易用的工具以支持各种常见的数值计算任务。它们可以与 STL 中的容器如 vector、list 等紧密结合通过迭代器方便地访问容器中的元素。同时STL 数值算法的底层实现经过优化确保了高效的运行性能。
2 std::accumulate
std::accumulate 用于计算给定范围内元素的累积值。这个算法特别适用于数值计算但也可以通过提供自定义的二元操作来执行更复杂的累积任务。
2.1 基本用法
std::accumulate 的基本用法是计算一个序列中所有元素的和。它接受三个参数范围的起始迭代器、范围的结束迭代器和一个初始值。
基本语法如下
templateclass InputIt, class T
T accumulate(InputIt first, InputIt last, T init);first 和 last 是定义输入序列范围的迭代器。init 是累积运算的初始值。默认的操作是加法所以如果没有提供自定义操作std::accumulate 会将序列中的所有元素加起来并返回总和。
如下为使用示例
#include numeric
#include vector
#include iostream int main() {std::vectorint numbers { 1, 2, 3, 4, 5 };int sum std::accumulate(numbers.begin(), numbers.end(), 0);std::cout Sum of the numbers: sum std::endl; return 0;
}上面代码的输出为
Sum of the numbers: 15在这个例子中std::accumulate 计算了向量 numbers 中所有元素的总和。
2.2 自定义操作
除了默认的加法操作std::accumulate 还允许你提供一个自定义的二元操作。这个操作应该接受两个参数并返回它们的某种累积结果。这使得 std::accumulate 可以用于执行更复杂的任务比如计算乘积、最大值、最小值等。
带有自定义操作的 std::accumulate 的语法如下
templateclass InputIt, class T, class BinaryOperation
T accumulate(InputIt first, InputIt last, T init, BinaryOperation op);op 是一个二元操作函数或函数对象它定义了如何进行累积。
如下为计算乘积的使用示例
#include numeric
#include vector
#include iostream
#include functional int main() {std::vectorint numbers { 1, 2, 3, 4, 5 };int product std::accumulate(numbers.begin(), numbers.end(), 1, std::multipliesint());std::cout Product of the numbers: product std::endl; return 0;
}上面代码的输出为
Product of the numbers: 1202.3 自定义数据结构
std::accumulate 不仅可以用于基本数据类型的序列也可以用于自定义数据结构的序列。通过提供自定义的二元操作std::accumulate 可以对自定义数据结构的对象执行复杂的累积操作。
自定义数据结构
首先假设有一个自定义的数据结构比如一个表示点的类 Point它包含 x 和 y 两个坐标
class Point {
public: int x, y; Point(int x 0, int y 0) : x(x), y(y) {} // 可以添加其他成员函数比如用于输出、比较等
};自定义二元操作
对于上面的自定义数据结构std::accumulate 无法直接使用默认的加法操作进行累积。因此需要提供一个自定义的二元操作告诉 std::accumulate 如何合并两个 Point 对象。
假设想要计算一系列点的 x 坐标的总和和 y 坐标的总和可以定义一个二元操作函数或函数对象来实现这一点
struct PointAccumulator { Point operator()(const Point a, const Point b) const { return Point(a.x b.x, a.y b.y); }
};这里PointAccumulator 是一个函数对象它重载了调用运算符 operator() 来接受两个 Point 对象并返回一个新的 Point 对象其中 x 和 y 坐标分别是输入点对应坐标的和。
使用 std::accumulate
现在可以使用 std::accumulate 和自定义的二元操作来计算一系列 Point 对象的累积和
#include iostream
#include vector
#include numeric // 自定义数据结构
class Point {
public: int x, y; Point(int x 0, int y 0) : x(x), y(y) {} // 输出点 void print() const { std::cout ( x , y ) std::endl; }
}; // 自定义二元操作
struct PointAccumulator { Point operator()(const Point a, const Point b) const { return Point(a.x b.x, a.y b.y); }
}; int main() { // 创建包含点的向量 std::vectorPoint points { Point(1, 2), Point(3, 4), Point(5, 6) }; // 使用 std::accumulate 和自定义二元操作计算累积和 Point sum std::accumulate(points.begin(), points.end(), Point(), PointAccumulator()); // 输出结果 sum.print(); // 输出: (9, 12) return 0;
}上面代码的输出为
(9, 12)这个例子创建了一个包含三个 Point 对象的 std::vector。然后使用 std::accumulate 来计算这些点的累积和。代码中给 std::accumulate 函数传递了向量的起始和结束迭代器一个初始的 Point 对象这里是一个默认构造的 Point以及的自定义二元操作 PointAccumulator 的一个实例。随后 std::accumulate 遍历向量中的每个点并使用 PointAccumulator 来合并它们最终得到一个表示所有点坐标和的 Point 对象。
注意事项
自定义二元操作必须能够处理你的数据结构并返回正确的累积结果。初始值对于 std::accumulate 的结果很重要。对于自定义数据结构需要提供一个合适的初始值。在这个例子中使用了 Point 的默认构造函数来创建一个初始点。如果自定义的数据结构包含非数值类型的成员比如字符串或自定义对象则需要确保二元操作能够适当地处理这些成员。
通过自定义二元操作std::accumulate 可以适应各种复杂的累积需求包括处理自定义数据结构的序列。这使得它成为一个非常强大且灵活的算法工具。
3 std::inner_product
std::inner_product 用于计算两个序列的点积也称为内积。点积是一个数学概念常用于向量运算中其结果是一个标量表示两个向量的相似度或夹角。在 C 中std::inner_product 可以应用于任何支持基本算术运算的类型包括但不限于整数、浮点数以及自定义类型。
3.1 基本用法
std::inner_product 的基本语法如下
templateclass InputIt1, class InputIt2, class T
T inner_product(InputIt1 first1, InputIt1 last1, InputIt2 first2, T init);基本用法中std::inner_product 会将两个序列中对应位置的元素相乘并将所有乘积相加最后返回总和。默认操作是乘法和加法。
#include iostream
#include vector
#include numeric int main() {std::vectorint v1 { 1, 2, 3 };std::vectorint v2 { 4, 5, 6 };int result std::inner_product(v1.begin(), v1.end(), v2.begin(), 0);std::cout Inner product: result std::endl; return 0;
}上面代码的输出为
Inner product: 32上面示例中 std::inner_product 计算的是 1*4 2*5 3*6 的结果。
3.2 自定义操作
std::inner_product 允许为序列元素的合并和乘积的累加提供自定义的二元操作其语法如下
templateclass InputIt1, class InputIt2, class T, class BinaryOperation1, class BinaryOperation2
T inner_product(InputIt1 first1, InputIt1 last1, InputIt2 first2, T init, BinaryOperation1 binary_op1, BinaryOperation2 binary_op2);first1, last1定义第一个输入序列范围的迭代器。first2指向第二个输入序列起始位置的迭代器。init累积运算的初始值。binary_op1一个二元操作用于合并第一个序列中的元素。默认为加法。binary_op2一个二元操作用于合并两个序列中对应位置的元素。默认为乘法。
可以通过以下方式提供自定义操作
使用函数对象functor或 lambda 表达式
可以定义一个函数对象一个带有 operator() 的类或使用 C11 引入的 lambda 表达式。
使用标准库中的函数对象
C 标准库提供了一些函数对象如 std::plus, std::minus, std::multiplies 等可以直接使用它们作为自定义操作。
使用函数指针
如果函数符合二元操作的签名即接受两个参数并返回一个结果也可以使用函数指针作为自定义操作。
使用自定义操作的示例
假设有两个整数向量需要计算它们的“点差积”即对应元素的差的平方和而不是通常的点积。
#include iostream
#include vector
#include numeric
#include functional // for std::plus and std::minus // 自定义的二元操作计算平方
struct Square {int operator()(int x) const {return x * x;}
};int main() {std::vectorint v1 { 1, 2, 3 };std::vectorint v2 { 4, 5, 6 };// 使用自定义操作计算点差积 int result std::inner_product(v1.begin(), v1.end(), v2.begin(), 0,std::plus(), // 默认的加法用于累加结果 [](int a, int b) { return (a - b) * (a - b); }); // lambda 表达式计算差的平方 std::cout Point difference product: result std::endl; // 输出点差积的结果 return 0;
}上面代码的输出为
Point difference product: 27这个例子使用了 std::plus 作为 binary_op1 来累加结果但使用了 lambda 表达式作为 binary_op2 来计算两个对应元素的差的平方。这样std::inner_product 就会计算 (1-4)^2 (2-5)^2 (3-6)^2 的结果。
使用标准库中的函数对象的示例
如果不想定义新的函数对象或 lambda 表达式也可以直接使用标准库提供的函数对象。例如使用 std::minus 和 std::multiplies 来计算两个序列对应元素之差的乘积和
#include iostream
#include vector
#include numeric
#include functional // for std::plus, std::minus and std::multiplies int main() {std::vectorint v1 { 1, 2, 3 };std::vectorint v2 { 4, 5, 6 };// 使用标准库函数对象计算对应元素之差的乘积和 int result std::inner_product(v1.begin(), v1.end(), v2.begin(), 0,std::plus(), // 默认的加法用于累加结果 std::multiplies()); // 默认的乘法用于计算对应元素的乘积 std::cout Sum of differences product: result std::endl; // 输出对应元素之差的乘积和的结果 return 0;
}上面代码的输出为
Sum of differences product: 32这个例子使用了 std::plus 作为 binary_op1 来累加结果但使用了 std::multiplies 作为 binary_op2 来计算两个对应元素的乘积。这样std::inner_product 就会计算 (1*4) (2*5) (3*6) 的结果。
3.3 自定义数据结构
std::inner_product 是一个通用的算法它并不直接依赖于特定的数据结构。它接收两个迭代器范围通常是容器的开始和结束迭代器以及两个二元操作函数对象或函数指针。因此开发者可以使用 std::inner_product 来计算任何可迭代的数据结构中元素的某种累积操作只要提供正确的迭代器和合适的自定义操作。
然而对于更复杂的数据结构比如自定义类可能需要定义合适的操作来匹配 std::inner_product 的期望。这通常意味着需要定义如何合并两个对象binary_op1以及如何计算两个对象之间的某种关系binary_op2。
下面是一个例子展示了如何为自定义的数据结构使用 std::inner_product
#include iostream
#include vector
#include numeric
#include functional // for std::plus // 自定义的数据结构
struct Point {double x, y;// 假设我们想要计算两个点的内积 Point operator*(const Point other) const {return { x * other.x, y * other.y };}// 假设我们想要累加点的某个属性比如 x 坐标的和 Point operator(const Point other) const {return { x other.x, y other.y };}
};int main() {std::vectorPoint points { {1, 2}, {3, 4}, {5, 6} };// 初始化一个用于累加的点这里我们初始化为 {0, 0} Point initial_sum { 0, 0 };// 使用 std::inner_product 计算所有点的内积和 // 这里我们不需要自定义 binary_op1因为 Point 类型已经定义了 操作符 // 但我们需要自定义 binary_op2 来计算两个点的内积 Point result std::inner_product(points.begin(), points.end(), points.begin(), initial_sum,std::plus(),[](const Point a, const Point b) { return a * b; });std::cout Sum of inner products: ( result.x , result.y ) std::endl;return 0;
}上面代码的输出为
Sum of inner products: (35, 56)在这个例子中Point 结构体定义了两个操作符operator* 用于计算两个点的内积operator 用于累加点的坐标。当调用 std::inner_product 时这里传递了 std::plus 作为 binary_op1它将用于累加点的坐标。同时这里也提供了一个 lambda 表达式作为 binary_op2它使用 operator* 来计算两个点的内积。
注意这里的 binary_op1 和 binary_op2 的参数和返回类型需要与迭代器所指向的数据类型在这里是 Point兼容。binary_op1 应该接受两个 Point 对象并返回一个 Point 对象而 binary_op2 应该接受两个 Point 对象并返回一个可以用于累加的值在这里是一个 double 类型的内积。
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/88772.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!