如何在`fold expressions`中提供`short-circuit evaluation`?(How to make `short-circuit evaluation` also available in `fold expressions`?)
#include <type_traits> #define FORWARD(arg)\ std::forward<decltype(arg)>(arg) template<typename... Args> constexpr bool AndL(Args&&... args) { return (... && FORWARD(args)); } template<typename... Args> constexpr bool AndR(Args&&... args) { return (FORWARD(args) && ...); } int main() { bool* pb = nullptr; false && (*pb = true); // ok at runtime. AndL(false, (*pb = true)); // error at runtime! AndR(false, (*pb = true)); // error at runtime! }
传统的
&&
操作符支持短路评估 ,因此在运行时false && (*pb = true)
将会正常,但以下两种情况不会。
fold expressions
如何进行短路评估 ?#include <type_traits> #define FORWARD(arg)\ std::forward<decltype(arg)>(arg) template<typename... Args> constexpr bool AndL(Args&&... args) { return (... && FORWARD(args)); } template<typename... Args> constexpr bool AndR(Args&&... args) { return (FORWARD(args) && ...); } int main() { bool* pb = nullptr; false && (*pb = true); // ok at runtime. AndL(false, (*pb = true)); // error at runtime! AndR(false, (*pb = true)); // error at runtime! }
The traditional
&&
operator supports short-circuit evaluation, sofalse && (*pb = true)
will be ok at runtime, but the following two cases are not.How to make short-circuit evaluation also available in
fold expressions
?
原文:https://stackoverflow.com/questions/43047858
最满意答案
看起来你的函数可能有两个问题。 1.)您将重置用于跟踪每次迭代信息的变量。 2.)在每次迭代期间,您只将每个学生与同一名学生进行比较。 因此,我认为像下面这样的东西会提供你正在寻找的东西。
function hghGrade(json) { if (json.length > 1) { var text = ""; var maxGrade = 0; var thisGrade = 0; for (var i = 0; i < json.length; i++) { thisGrade = parseFloat(json[i].grade); if (maxGrade < thisGrade)) { maxGrade = thisGrade; test = json[i].names + " ID code " + json[i].code + " with grade " + json[i].grade; } } alert("The best grade is from: " + text); } else { alert("Please register at least two students"); } }
It looks like you might have two problems in your functions. 1.) You are resetting the variables used to track the information with each iteration. 2.) You are only comparing each student to the same student, during each iteration. Thus, I think something like the below would provide what you are looking for.
function hghGrade(json) { if (json.length > 1) { var text = ""; var maxGrade = 0; var thisGrade = 0; for (var i = 0; i < json.length; i++) { thisGrade = parseFloat(json[i].grade); if (maxGrade < thisGrade)) { maxGrade = thisGrade; test = json[i].names + " ID code " + json[i].code + " with grade " + json[i].grade; } } alert("The best grade is from: " + text); } else { alert("Please register at least two students"); } }
相关问答
更多-
要在数组中获取最小/最大值,可以使用: var _array = [1,3,2]; Math.max.apply(Math,_array); // 3 Math.min.apply(Math,_array); // 1 To get min/max value in array, you can use: var _array = [1,3,2]; Math.max.apply(Math,_array); // 3 Math.min.apply(Math,_array); // 1
-
没有办法在一般情况下找到最大/最小值,而不循环遍历所有n个元素(如果从1到n-1,您如何知道元素n是否不大于(或更小)当前最大/最小)? 你提到这些值每两秒钟改变一次。 如果您确切知道哪些值发生变化,您可以从先前的最大/最小值开始,仅与新的值进行比较,但即使在这种情况下,如果修改的值之一是您的最大/最小值,则可以需要再循环通过它们。 另一个替代方法 - 再次,只有变化的值的数量很小 - 将值存储在诸如树或堆的结构中,并且当新值到达时,您将适当地插入(或更新)它们。 但是您是否可以做到这一点,根据您的问题不清 ...
-
这是一个非常简单的例子,您可以调整以适合您的目的: // create this array outside of for loop so it already exists var arr = []; // test data var a = { user:"Ron", date:"2014-08-01", weight:"192"}; var b = { user:"Ron", date:"2014-08-02", weight:"195"}; var c = { user:"Ron", date: ...
-
生成具有动态最小值和最大值的动态jQuery Slider(Generating Dynamic jQuery Slider with dynamic min and max values)[2022-03-29]
这是因为你的JSON数据是一个字符串,除去那些值的引号,例如 { "photographer_id": "2", "label": "Tommy Stay (TS)", "value": "TS", "rollnum_from": 1, "rollnum_to": 10 } 代替 { "photographer_id": "2", "label": "Tommy Stay (TS)" ... -
从Thinking Sphinx搜索获得最小值和最大值[重复](Getting min and max values from Thinking Sphinx search [duplicate])[2022-03-01]
请访问http://sphinxsearch.com/docs/manual-2.0.7.html#api-func-setselect 使用sphinxQL它是可能的 使用SphinxSE时要小心,你可以加入真实的数据和计算...... Have a look to http://sphinxsearch.com/docs/manual-2.0.7.html#api-func-setselect with sphinxQL it's possible With SphinxSE be carefull, ... -
看起来你的函数可能有两个问题。 1.)您将重置用于跟踪每次迭代信息的变量。 2.)在每次迭代期间,您只将每个学生与同一名学生进行比较。 因此,我认为像下面这样的东西会提供你正在寻找的东西。 function hghGrade(json) { if (json.length > 1) { var text = ""; var maxGrade = 0; var thisGrade = 0; for (var i = 0; i < json ...
-
原生JS解决方案: var t =[ { "room": { "price": 217, "available": true } }, { "room": { "price": 302, "available": true, } }, { "room": { "price": 427, "available": true, } } ] var min = t.map(function(el){return el.room.price}).reduce(function(el){return Math.min ...
-
如果我们在这里使用一些函数式编程而不是for循环,那将非常有趣: d3.extent(dataset.reduce(function(a,b) { return a.concat(b.year) }, [])) 你可以用值做同样的事情。 It's very fun if we use a little functional programming instead of for loops here: d3.extent(dataset.reduce(function(a,b) { return a.con ...
-
d3.js - 具有值数组的json数据的最大值和最小值(d3.js - max and min value from json data which has array of values)[2023-08-20]
d3.max函数需要有一个获取单个值的访问器。 您需要嵌套调用,因为您有嵌套数据: var max = d3.max(data, function(d) { return d3.max(d.Checkintimes, function(e) { return d3.max(e); }); }); The d3.max function needs to have an accessor that gets individual values. You would need nested calls a ... -
在JavaScript / HTML中访问JSON数组中的最小值和最大值(Accessing min and max value within JSON array in JavaScript/HTML)[2023-03-17]
这个怎么样? 由于您的代码示例不完整,我无法自行测试。 function createJSON(name, data){ var min = Number.MAX_SAFE_INTEGER; var max = Number.MIN_SAFE_INTEGER; for(var i = 0; i < data.length; i++) { if(data[i].value < min) min = data[i].value; if(data[i].value > max) max ...