使用变换和 plus<int>() 对向量的元素求和并求模 10^9+7。有什么办法可以这样做吗?
Sum and modulo 10^9+7 of elements of the vector using transform and plus<int>(). Is there any way to do like this?
我想添加两个向量的元素(小值示例 V1={1,2,3} V2{4,5,6} 然后 V3={5,7,9})并且还想取模 10^9+7 有没有更快(比循环)的方法??
您可以使用 lambda:
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 函数。
我想添加两个向量的元素(小值示例 V1={1,2,3} V2{4,5,6} 然后 V3={5,7,9})并且还想取模 10^9+7 有没有更快(比循环)的方法??
您可以使用 lambda:
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 函数。