开发者

C++: Comparing list of doubles with some invalid values (QNAN)

i need to compare two std::list < double >, bu开发者_开发百科t some doubles may be invalid numbers (QNAN). If any invalid numbers are list entries the compare process won't work, because a comparison of the same invalid value will always result in 'false'. What is the easiest and most elegant way to solve the problem?

My idea was to create copies of both lists, iterate through them and remove invalid values and then compare the remaining lists. The lists will typically have 20-50 values in them. Is there a more resource friendly way to solve it?


bool compare(float f1, float f2)
{
    return (f1 != f1 && f2 != f2)? true: f1 == f2;
}

std::list<float> l1, l2;
bool res = std::equal(l1.begin(), l1.end(), l2.begin(), compare);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜