我有一个 std::vector<double>可能包含多个 NAN值。我想找到 vector 中最大的元素。我怎样才能有效地跳过 NAN在比较中?我想避免调用 isnan在每个元素上。有什么想法吗?
// std::max_element([NAN,NAN,NAN,-31,-89]) = NAN
// because NAN > -31 returns NAN.
// how can I skip all NANs in the comparison?
// test 2 below is my use case.
#include <vector>
#include <iostream>
#include <cmath>
void vector_max(std::vector<double> v, double &max, int &imax){
std::vector<double>::iterator v_iter;
v_iter = std::max_element(v.begin(),v.end());
imax = std::distance(v.begin(), v_iter);
max = *v_iter;
}
int main(){
std::vector<double> v_vec;
std::vector<double>::iterator v_vec_iter;
int imax;
double val;
std::cout << "test 1. " << std::endl;
v_vec.push_back( -33.0 );
v_vec.push_back( -124.0 );
v_vec.push_back( -31.0 );
v_vec.push_back( 18.4 );
vector_max(v_vec,val,imax);
std::cout << "max(v_vec) = " << val << std::endl;
std::cout << "indmax(v_vec) = " << imax << std::endl;
std::cout << "test 2: my case. " << std::endl;
v_vec.clear();
v_vec.push_back( NAN );
v_vec.push_back( NAN );
v_vec.push_back( NAN );
v_vec.push_back( -33.0 );
v_vec.push_back( -124.0 );
v_vec.push_back( -31.0 );
v_vec.push_back( 31.0 );
vector_max(v_vec,val,imax);
std::cout << "max(v_vec) = " << val << std::endl;
std::cout << "indmax(v_vec) = " << imax << std::endl;
};
返回:
test 1.
max(v_vec) = 18.4
indmax(v_vec) = 3
test 2.
max(v_vec) = nan
indmax(v_vec) = 0
最佳答案
您可以为 max_element 提供自定义比较:
void vector_max(std::vector<double> v, double &max, int &imax){
std::vector<double>::iterator v_iter;
v_iter = std::max_element(v.begin(),v.end(),
[] (auto x, auto y)
{
return x < y ? true : isnan(x);
});
imax = std::distance(v.begin(), v_iter);
max = *v_iter;
}
关于c++ - std::max_element 跳过 NAN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35889529/
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
我可以得到Infinity和NaNn=9.0/0#=>Infinityn.class#=>Floatm=0/0.0#=>NaNm.class#=>Float但是当我想直接访问Infinity或NaN时:Infinity#=>uninitializedconstantInfinity(NameError)NaN#=>uninitializedconstantNaN(NameError)什么是Infinity和NaN?它们是对象、关键字还是其他东西? 最佳答案 您看到打印为Infinity和NaN的只是Float类的两个特殊实例的字符串
当我的预订模型通过rake任务在状态机上转换时,我试图找出如何跳过对ActiveRecord对象的特定实例的验证。我想在reservation.close时跳过所有验证!叫做。希望调用reservation.close!(:validate=>false)之类的东西。仅供引用,我们正在使用https://github.com/pluginaweek/state_machine用于状态机。这是我的预订模型的示例。classReservation["requested","negotiating","approved"])}state_machine:initial=>'requested
如何将send与+=一起使用?a=20;a.send"+=",10undefinedmethod`+='for20:Fixnuma=20;a+=10=>30 最佳答案 恐怕你不能。+=不是方法,而是语法糖。参见http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html它说Incommonwithmanyotherlanguages,Rubyhasasyntacticshortcut:a=a+2maybewrittenasa+=2.你能做的最好的事情是:
在读取/解析文件(使用Ruby)时忽略某些行的最佳方法是什么?我正在尝试仅解析Cucumber.feature文件中的场景,并希望跳过不以Scenario/Given/When/Then/And/But开头的行。下面的代码有效,但它很荒谬,所以我正在寻找一个聪明的解决方案:)File.open(file).each_linedo|line|line.chomp!nextifline.empty?nextifline.include?"#"nextifline.include?"Feature"nextifline.include?"Inorder"nextifline.include?
我正在尝试将以下SQL查询转换为ActiveRecord,它正在融化我的大脑。deletefromtablewhereid有什么想法吗?我想做的是限制表中的行数。所以,我想删除少于最近10个条目的所有内容。编辑:通过结合以下几个答案找到了解决方案。Temperature.where('id这给我留下了最新的10个条目。 最佳答案 从您的SQL来看,您似乎想要从表中删除前10条记录。我相信到目前为止的大多数答案都会如此。这里有两个额外的选择:基于MurifoX的版本:Table.where(:id=>Table.order(:id).
我对如何计算通过{%assignvar=0%}赋值的变量加一完全感到困惑。这应该是最简单的任务。到目前为止,这是我尝试过的:{%assignamount=0%}{%forvariantinproduct.variants%}{%assignamount=amount+1%}{%endfor%}Amount:{{amount}}结果总是0。也许我忽略了一些明显的东西。也许有更好的方法。我想要存档的只是获取运行的迭代次数。 最佳答案 因为{{incrementamount}}将输出您的变量值并且不会影响{%assign%}定义的变量,我
有没有办法跳过CSV文件的第一行,让第二行作为标题?我有一个CSV文件,第一行是日期,第二行是标题,所以我需要能够在遍历它时跳过第一行。我尝试使用slice但它会将CSV转换为数组,我真的很想将其读取为CSV,以便我可以利用header。 最佳答案 根据您的数据,您可以使用另一种方法和skip_lines-option此示例跳过所有以#开头的行require'csv'CSV.parse(DATA.read,:col_sep=>';',:headers=>true,:skip_lines=>/^#/#Markcomments!)do|
我有一个数组数组,想将元素附加到子数组。+=做我想做的,但我想了解为什么push不做。我期望的行为(并与+=一起工作):b=Array.new(3,[])b[0]+=["apple"]b[1]+=["orange"]b[2]+=["frog"]b=>[["苹果"],["橙子"],["Frog"]]通过推送,我将推送的元素附加到每个子数组(为什么?):a=Array.new(3,[])a[0].push("apple")a[1].push("orange")a[2].push("frog")a=>[[“苹果”、“橙子”、“Frog”]、[“苹果”、“橙子”、“Frog”]、[“苹果”、“
有没有办法让Ruby能够做这样的事情?classPlane@moved=0@x=0defx+=(v)#thisiserror@x+=v@moved+=1enddefto_s"moved#{@moved}times,currentxis#{@x}"endendplane=Plane.newplane.x+=5plane.x+=10putsplane.to_s#moved2times,currentxis15 最佳答案 您不能在Ruby中覆盖复合赋值运算符。任务在内部处理。您应该覆盖+,而不是+=。plane.a+=b与plane.a=