使用 STL 将向量元素乘以标量值
您好,我想通过标量值进行(乘法、加法等)向量,例如 myv1 * 3
,我知道我可以使用 forloop 执行一个函数,但是有没有办法使用 STL 函数来执行此操作?类似于 {Algorithm.h::transform function } 之类的东西?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
是的,使用
std::transform
:在 C++17 之前,您可以使用
std::bind1st()
,它在 C++11 中已弃用。对于占位符;
Yes, using
std::transform
:Before C++17 you could use
std::bind1st()
, which was deprecated in C++11.For the placeholders;
如果您可以使用
valarray
而不是vector
,它具有用于执行标量乘法的内置运算符。如果您必须使用
向量
,您确实可以使用transform
来完成这项工作:(假设您有类似于Boost.Lambda 允许您轻松创建匿名函数对象,例如
_1 * 3
:-P)If you can use a
valarray
instead of avector
, it has builtin operators for doing a scalar multiplication.If you have to use a
vector
, you can indeed usetransform
to do the job:(assuming you have something similar to Boost.Lambda that allows you to easily create anonymous function objects like
_1 * 3
:-P)现代 C++ 解决您的问题。
Modern C++ solution for your question.
我认为当您想要遍历向量并根据某种模式操作每个元素时,
for_each
非常合适,在这种情况下,一个简单的 lambda 就足够了:请注意,您想要为 lambda 函数捕获的任何变量要使用(例如,您想与某个预定标量相乘),请放入括号中作为参考。
I think
for_each
is 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: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.
如果您必须将结果存储在新向量中,那么您可以使用 标头中的 rel="nofollow noreferrer"> :
std::transform()
因此,我们在这里所说的是,
vec_input
的值(element
s)从开头 (vec_input.begin()
) 到结尾 (vec_input.begin( )
),[beginning, end)
),范围
元素
传递给最后一个参数,lambda表达式,vec_output
中(vec_output.begin( )
)。lambda 表达式
[&scale]
) 的值,vec_input
中。最后注意事项:虽然没有必要,但您可以传递下面的 lambda 表达式:
它明确指出 lambda 表达式的输出是双精度型。但是,我们可以忽略这一点,因为在这种情况下,编译器可以自行推断出返回类型。
If you had to store the results in a new vector, then you could use the
std::transform()
from the<algorithm>
header:So, what we are saying here is,
element
s) ofvec_input
starting from the beginning (vec_input.begin()
) to the end (vec_input.begin()
),[beginning, end)
) to transform,range
element
to the last argument, lambda expression,vec_output
starting from the beginning (vec_output.begin()
).The lambda expression
[&scale]
) from outside by reference,std::transform()
)vec_input
.Final note: Although unnecessary, you could pass lambda expression per below:
It explicitly states that the output of the lambda expression is a double. However, we can omit that, because the compiler, in this case, can deduce the return type by itself.
我知道这不是您想要的 STL,但您可以根据不同的需求进行调整。
以下是您可以用来计算的模板; “func”是您想要执行的函数:乘法、加法等; “parm”是“func”的第二个参数。您可以轻松扩展它以采用具有更多不同类型参数的不同函数。
另外,这不是一个“安全”函数,您必须在使用它之前进行类型/值检查等。
I know this not STL as you want, but it is something you can adapt as different needs arise.
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.
Also, this is not a 'safe' function, you must do type/value-checking etc. before you use it.