C ++,在输出时将NaN移到数组的末尾 [英] C++, moving a NaN to the end of the array, when output

查看:50
本文介绍了C ++,在输出时将NaN移到数组的末尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我制作了一个能够对数组进行排序的程序,并且我正在尝试对包含双FP的数组进行排序,包括我输入的2-3个随机FP,pos inf,neg inf和单个NaN.为此,我希望对NaN进行排序.所以我的代码有效,但是当尝试对NaN进行排序时,我无法这样做.我想做的是将其排序到末尾,或将其放在已排序数组的末尾.无论如何,我实际上可以做到这一点吗?提前致谢!!!代码如下:

So, i've made a program which is able to sort arrays, and i'm trying to sort an array containing double FP's, including 2-3 random ones i enter, pos inf, neg inf and a single NaN. so for this purpose i wish to sort the NaN. So my code works, however when trying to sort the NaN, i'm unable to do so. What i'd like to do is sort it to the end, or have it put at the end of the sorted array. Is there anyway I can actually do this? Thanks in advance!!! code is as follows:

int main()
{
int start_s = clock();
int n, k = 4, j; // k is number of elements
double x = -0.0;
double i = 0;
double swap = 0;//used in the function as a place holder and used for swapping between other variables
double a[100] = { (1/x) + (1/i), 2.3, 1/x *0, 1/i };//array of double elements // 1/i * 0 is NaN
//(1 / i) * 0


for (n = 0; n < (k - 1); n++) // for loop consists of variables and statements in order to arrange contents of array
{
  for (j = 0; j < k - n - 1; j++)
    {
      if (a[j] > a[j + 1])

        {
        swap = a[j];
        a[j] = a[j + 1];
        a[j + 1] = swap;

        }
    }
}

cout << "The list of sorted elements within the array, is: " << endl; /* Output message to user */
for (int i = 0; i < k; i++)// Loop up to number of elements within the array
{
    cout << a[i] << " ";/* Output contents of array */
}
cout << endl; //new line

int stop_s = clock();
cout << "The execution time of this sort, is equal to: " << (stop_s - start_s) / double(CLOCKS_PER_SEC) * 1000 << " milliseconds" << endl;



return 0; 

推荐答案

由于您仍然处于C ++领域,所以为什么不充分使用它.实际上,首先移动NaN,然后​​进行排序.我从您的代码中消除了噪音"并产生了它,然后编译并运行(在 gcc-4.4.3 上).主要区别是NaN位于开头,但很容易跳过,因为您会获得指向非NaN开头的指针.

Since you're in C++ land anyway, why not use it to the full. First, indeed, move the NaN's and then sort. I've taken out 'noise' from your code and produced this, it compiles and runs (edit: on gcc-4.4.3). The main difference is that the NaN's are at the beginning but they're easily skipped since you will get a pointer to the start of non-NaN's.

#include <iostream>
#include <algorithm>
#include <math.h>

int main()
{
    int n, k = 4, j; // k is number of elements
    double x = -0.0;
    double i = 0;
    double a[100] = { (1/x) + (1/i), 2.3, 1/x *0, 1/i };//array of double elements // 1/i * 0 is NaN]
    double *ptr; // will point at first non-NaN double

    // divide the list into two parts: NaN's and non-NaN's
    ptr = std::partition(a, a+k, isnan);
    // and sort 'm
    // EDIT: of course, start sorting _after_ the NaNs ...
    std::sort(ptr, a+k);

    cout << "The list of sorted elements within the array, is: " << endl; /* Output message to user */
    for (int i = 0; i < k; i++)// Loop up to number of elements within the array
    {
        cout << a[i] << " ";/* Output contents of array */
    }
    cout << endl; //new line

    return 0;
}

这篇关于C ++,在输出时将NaN移到数组的末尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆