根据最小和查找数组的元素
我用 C++ 编写了一个循环来给我 6 个随机数并将它们存储在一个数组中。 我想做的是对数组的元素求和,直到得到一个大于数字“x”的值,但我想这样做而不需要添加所有元素。 目标是找到第一个总和等于 x 值的元素。
例如,数组是 [1,2,3,4,5,6]
和 x = 6
,所以我要寻找的是元素 [1,2,3]
。
我查看了标准库并尝试使用“valarray”中的 sum 函数,但这只是给出了所有元素的总和。 任何有关如何成功编码的想法将不胜感激。
I've written a loop in C++ to give me 6 random numbers and store them in an array.
What I would like to do is to sum the elements of the array until I get a value larger than a number, "x", but I would like to do this without necessarily adding all the elements. The objective is to find the first elements which sum to the value of x.
For example, array is [1,2,3,4,5,6]
, and x = 6
, so what I would be looking for are the elements [1,2,3]
.
I've looked at the standard library and have tried using the sum function from "valarray" but this just gives the sum of all the elements. Any ideas on how to code this successfully would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
编写一个进行加法运算的函子。
Write a functor that does the addition.
我假设您只想要数组中的前 X 个元素,直到它们的总和达到或超过阈值(那里的问题有点模糊)。
如果是这样,我不知道如何在没有自己的循环的情况下做到这一点:
现在“i”包含总和达到或超过阈值的索引。
I'm assuming you just want the first X elements in the array, up until their sum meets or exceeds a threshold (the question was a little vague there).
If so, I don't know how to do that without your own loop:
Now "i" contains the index at which the sum met or exceeded your threshold.
避免建议将 find_if 与有状态谓词一起使用的答案。 有状态谓词很危险,因为 STL 算法假定复制谓词是安全的。 在这种情况下,如果对谓词进行副本,则每个副本将具有不同的“运行总计”,并且不一定会作用于所有值或按正确的顺序。
特别要避免将其谓词的operator()成员实现为const成员函数但将其成员标记为可变的解决方案,因为这会欺骗您认为它不是有状态谓词,这是不好的。
我建议使用简单循环查找答案的答案之一,或使用累加器的答案,因为这是最正确的方法(即使代码看起来有点笨拙)。
请注意警告可能不适用于 C 数组和 find_if;我只是不想让您知道有状态谓词是解决您的问题的正确方法,因为您最终可能会在将来危险的情况下使用该错误的解决方案
。 :C++ 编码标准:101 条规则、指南和最佳实践,第 87 项
Avoid the answers that suggest using find_if with a stateful predicate. Stateful predicates are dangerous as the STL algorithms assume it is safe to copy predicates. In this case, if copies are made of the predicate then each will have a different 'running total' and will not necessarily act on all values, or in the correct order.
Especially avoid the solution that implements its predicate's operator() member as a const member function but labels its members as mutable as this is fooling you into thinking it is not a stateful predicate, which is bad.
I'd suggest using either one of the answers that simply loops to find the answer, or the answer that uses an accumulator, as that is the most correct way to do it (even if the code looks a little unwieldy.
Note that the warnings may well not apply to C arrays and find_if; I just don't want you to learn that stateful predicates are the right way to solve your problem since you may end up using that incorrect solution in a situation where it is dangerous in future.
Reference: C++ Coding Standards: 101 Rules, Guidelines, and Best Practices, Item 87
这是一个稍微更通用的版本:
Here's a slightly more generic version:
将 x 中的数字一一减去,直到达到 0 或更低。
没有添加,如你所愿:)
Substract the numbers from x one by one, until you reach 0 or lower.
No additions, as you wished :)
希望这能起作用:
Here's hoping this works:
会是这样的:
would be something like:
好吧,我会使用向量
T 需要运算符+和运算符< 被定义为。
Well, i would use a vector
T would need operator+ and operator< to be defined.
您可以将 std::find_if() 与一个维护运行总计的函子一起使用,并且只有当您找到使您处于或超过顶部的元素时,才从函子返回 true。
例如:
You could use std::find_if() along with a functor that maintains a running total, and only returtn true from the functor when you have found the element that puts you at or over the top.
For example: