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

发布于 2024-09-26 05:36:17 字数 148 浏览 5 评论 0 原文

您好,我想通过标量值进行(乘法、加法等)向量,例如 myv1 * 3 ,我知道我可以使用 forloop 执行一个函数,但是有没有办法使用 STL 函数来执行此操作?类似于 {Algorithm.h::transform function } 之类的东西?

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 }?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

再浓的妆也掩不了殇 2024-10-03 05:36:17

是的,使用 std::transform

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

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

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

对于占位符;

#include <functional> 

Yes, using 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.

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

For the placeholders;

#include <functional> 
爱给你人给你 2024-10-03 05:36:17

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

v *= 3;

如果您必须使用向量,您确实可以使用transform来完成这项工作:(

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

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

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

v *= 3;

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

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

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

苄①跕圉湢 2024-10-03 05:36:17

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

#include <algorithm>
#include <vector>

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

Modern C++ solution for your question.

#include <algorithm>
#include <vector>

std::vector<double> myarray;
double myconstant{3.3};
std::transform(myarray.begin(), myarray.end(), myarray.begin(), [&myconstant](auto& c){return c*myconstant;});
吾性傲以野 2024-10-03 05:36:17

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

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

请注意,您想要为 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:

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.

奈何桥上唱咆哮 2024-10-03 05:36:17

如果您必须将结果存储在新向量中,那么您可以使用 标头中的 rel="nofollow noreferrer">std::transform()

#include <algorithm>
#include <vector>

int main() {
    const double scale = 2;
    std::vector<double> vec_input{1, 2, 3};
    std::vector<double> vec_output(3); // a vector of 3 elements, Initialized to zero
    // ~~~
    std::transform(vec_input.begin(), vec_input.end(), vec_output.begin(),
                   [&scale](double element) { return element *= scale; });
    // ~~~
    return 0;
}

因此,我们在这里所说的是,

  • vec_input 的值(elements)从开头 (vec_input.begin()) 到结尾 (vec_input.begin( )),
    • 本质上,使用前两个参数,您可以指定要转换的元素范围 ([beginning, end)),
      范围
  • 将每个元素传递给最后一个参数,lambda表达式,
  • 获取lambda表达式的输出并将其从头开始放入vec_o​​utput中(vec_o​​utput.begin( ))。
    • 第三个参数是指定目标向量的开头。

lambda 表达式

  • 通过引用从外部捕获比例因子 ([&scale]) 的值,
  • 将 double 类型的向量元素作为其输入(通过 std::transform 传递给它) ())
  • 在函数体中,它返回最终结果,
    • 正如我上面提到的,它将因此存储在 vec_input 中。

最后注意事项:虽然没有必要,但您可以传递下面的 lambda 表达式:

[&scale](double element) -> double { return element *= scale; }

它明确指出 lambda 表达式的输出是双精度型。但是,我们可以忽略这一点,因为在这种情况下,编译器可以自行推断出返回类型。

If you had to store the results in a new vector, then you could use the std::transform() from the <algorithm> header:

#include <algorithm>
#include <vector>

int main() {
    const double scale = 2;
    std::vector<double> vec_input{1, 2, 3};
    std::vector<double> vec_output(3); // a vector of 3 elements, Initialized to zero
    // ~~~
    std::transform(vec_input.begin(), vec_input.end(), vec_output.begin(),
                   [&scale](double element) { return element *= scale; });
    // ~~~
    return 0;
}

So, what we are saying here is,

  • take the values (elements) of vec_input starting from the beginning (vec_input.begin()) to the end (vec_input.begin()),
    • essentially, with the first two arguments, you specify a range of elements ([beginning, end)) to transform,
      range
  • pass each element to the last argument, lambda expression,
  • take the output of lambda expression and put it in the vec_output starting from the beginning (vec_output.begin()).
    • the third argument is to specify the beginning of the destination vector.

The lambda expression

  • captures the value of scale factor ([&scale]) from outside by reference,
  • takes as its input a vector element of type double (passed to it by std::transform())
  • in the body of the function, it returns the final result,
    • which, as I mentioned above, will be consequently stored in the vec_input.

Final note: Although unnecessary, you could pass lambda expression per below:

[&scale](double element) -> double { return element *= scale; }

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.

肥爪爪 2024-10-03 05:36:17

我知道这不是您想要的 STL,但您可以根据不同的需求进行调整。

以下是您可以用来计算的模板; “func”是您想要执行的函数:乘法、加法等; “parm”是“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 */

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

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文