[英]std::max_element skip over NANs
我有一個std::vector<double>
,其中可能包含多個NAN
值。 我想在向量中找到最大的元素。 如何有效地跳過比較中的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;
}
我會嘗試這樣的事情:
void vector_max(std::vector<double> v, double &max, int &imax){
std::vector<double>::size_type p=0;
imax = -1;
max = std::numeric_limits<double>::lowest();
for (auto &val : v)
{
if (!std::isnan(val) && val>max)
{
imax = p;
max = val;
}
p++;
}
}
問題是默認情況下std::max_element
使用std::less
作為其比較器。 根據它處理矢量元素的順序, NAN
可能會出現在比較的右側。 由於所有與NAN
的比較都返回false
,因此這意味着NAN
可能比所有其他元素都大。
換句話說,當在帶有NAN
的向量std::max_element
與默認比較器一起使用時,結果實際上是不確定的,因為它取決於實現和元素的順序。 例如,在GCC上,如果我將所有NAN
都放在向量的末尾 ,我(隨機地)會得到所需的結果。
因此,除了提供自己的比較運算符外,您別無選擇:
#include <vector>
#include <iostream>
#include <cmath>
#include <algorithm>
template <typename T>
struct NaNAwareLess
{
bool operator () (T a, T b) const
{
if (std::isnan(b))
{
return false; // Assume NaN is less than *any* non-NaN value.
}
if (std::isnan(a))
{
return true; // Assume *any* non-NaN value is greater than NaN.
}
return (a < b);
}
};
void vector_max(std::vector<double> v, double &max, int &imax){
std::vector<double>::iterator v_iter;
v_iter = std::max_element<std::vector<double>::iterator, NaNAwareLess<double> >(v.begin(),v.end(),NaNAwareLess<double>());
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;
std::cout << std::boolalpha << std::less<double>()(NAN, -33.0) << std::endl;
std::cout << std::boolalpha << std::less<double>()(-33.0, NAN) << std::endl;
};
我認為您無法避免致電isnan
。 而且還有另外一個重要的方面還有:從個人的經驗,我發現執行操作NAN
值比對(可能是因為FPU異常處理)任何其他值慢了許多 。 因此,雖然使用isnan
可能很煩人,但也可能對性能產生積極的影響。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.