使用变换和加 () 对向量的元素进行求和和模 10^9+7<int>。有什么办法可以做到这一点吗?

Sum and modulo 10^9+7 of elements of the vector using transform and plus<int>(). Is there any way to do like this?

本文关键字:什么 int lt gt 这一点 向量 变换 元素 求和      更新时间:2023-10-16

我想添加两个向量的元素(小价值示例v1 = {1,2,3} v2 {4,5,6},然后v3 = {5,7,9})并且还想服用modulo 10^9 7是否有任何更快的方法(比循环)?

您可以使用lambdas:

std::transform(v1.begin(), v1.end(), v2.begin(), std::back_inserter(v3),
               [](int x, int y) { return x + y; });

或std :: valarray而不是向量:

std::valarray<int> v1 = {1,2,3};
std::valarray<int> v2 = {4,5,6};
std::valarray<int> v3 = v1 + v2; // or other arithmetics

std::transform的另一种选择是使用std :: for_each函数。