C++ 使用 STL 将向量元素乘以标量值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3885095/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 13:57:45  来源:igfitidea点击:

Multiply vector elements by a scalar value using STL

c++stlvectorelementoperation

提问by Ismail Marmoush

Hi I want to (multiply,add,etc) vector by scalar value for example myv1 * 3, I know I can do a function with a forloop , but is there a way of doing this using STL function? Something like the {Algorithm.h :: transform function }?

嗨,我想(乘法、加法等)向量乘以标量值,例如myv1 * 3,我知道我可以用 forloop 做一个函数,但是有没有办法使用 STL 函数来做到这一点?类似于 {Algorithm.h :: 变换函数}?

回答by Oliver Charlesworth

Yes, using std::transform:

是的,使用std::transform

std::transform(myv1.begin(), myv1.end(), myv1.begin(),
               std::bind(std::multiplies<T>(), std::placeholders::_1, 3));

Before C++17 you could use std::bind1st(), which was deprecated in C++11.

在 C++17 之前,您可以使用std::bind1st(),它在 C++11 中已被弃用。

std::transform(myv1.begin(), myv1.end(), myv1.begin(),
               std::bind1st(std::multiplies<T>(), 3));

For the placeholders;

对于占位符;

#include <functional> 

回答by Chris Jester-Young

If you can use a valarrayinstead of a vector, it has builtin operators for doing a scalar multiplication.

如果您可以使用 avalarray而不是 a vector,则它具有用于进行标量乘法的内置运算符。

v *= 3;

If you have to use a vector, you can indeed use transformto do the job:

如果您必须使用 a vector,您确实可以使用它transform来完成这项工作:

transform(v.begin(), v.end(), v.begin(), _1 * 3);

(assuming you have something similar to Boost.Lambdathat allows you to easily create anonymous function objects like _1 * 3:-P)

(假设您有类似于Boost.Lambda 的东西,它允许您轻松创建匿名函数对象,例如_1 * 3:-P)

回答by Spandyie

Mordern C++ solution for your question.

您的问题的现代 C++ 解决方案。

std::vector<double> myarray;
double myconstant{3.3};
std::transform(myarray.begin(), myarray.end(), myarray.begin(), [&myconstant](auto& c){return c*myconstant;});

回答by slashmais

I know this not STL as you want, but it is something you can adapt as different needs arise.

我知道这不是你想要的 STL,但它是你可以随着不同需求的出现而适应的东西。

Below is a template you can use to calculate; 'func' would be the function you want to do: multiply, add, and so on; 'parm' is the second parameter to the 'func'. You can easily extend this to take different func's with more parms of varied types.

以下是您可以用来计算的模板;'func' 将是您想要执行的函数:乘法、加法等;“parm”是“func”的第二个参数。您可以轻松地扩展它以采用具有更多不同类型参数的不同 func。

template<typename _ITStart, typename _ITEnd, typename _Func , typename _Value >
_ITStart xform(_ITStart its, _ITEnd ite, _Func func, _Value parm)
{
    while (its != ite) { *its = func(*its, parm); its++; }
    return its;
}
...

int mul(int a, int b) { return a*b; }

vector< int > v;

xform(v.begin(), v.end(), mul, 3); /* will multiply each element of v by 3 */

Also, this is not a 'safe' function, you must do type/value-checking etc. before you use it.

此外,这不是一个“安全”功能,您必须在使用它之前进行类型/值检查等。

回答by storluffarn

I think for_eachis very apt when you want to traverse a vector and manipulate each element according to some pattern, in this case a simple lambda would suffice:

我认为for_each当您想遍历向量并根据某种模式操作每个元素时非常合适,在这种情况下,一个简单的 lambda 就足够了:

std::for_each(myv1.begin(), mtv1.end(), [](int &el){el *= 3; });

note that any variable you want to capture for the lambda function to use (say that you e.g. wanted to multiply with some predetermined scalar), goes into the bracket as a reference.

请注意,您想要为 lambda 函数捕获的任何变量(例如,您想要与某个预定标量相乘)都将作为参考放入括号中。