C++ Multiplying elements in a vector

前端 未结 2 2084
执笔经年
执笔经年 2021-01-01 20:59

I have been looking for a more optimal solution to the following and I cannot seem to find one.

Let\'s say I have a vector:

std::vector v

2条回答
  •  情书的邮戳
    2021-01-01 21:39

    You can use a ranged based for loop like:

    std::vector vars = {1, 2, 3}
    int multi = 1;
    
    for (const auto& e: vars)
        multi *= e;
    

提交回复
热议问题