Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何更改此项以检查并用值替换NAN。我还需要在行和列中填充空白部分_C++ - Fatal编程技术网

C++ 如何更改此项以检查并用值替换NAN。我还需要在行和列中填充空白部分

C++ 如何更改此项以检查并用值替换NAN。我还需要在行和列中填充空白部分,c++,C++,我有一个包含许多行和列的值的数据集,如何检查NAN的代码,并用值替换它们,并用“00”填充任何空格 我已经读取了文件,数据被有序地组织到列中,所以我想替换NaN,将行和列的总数相加,然后找到平均值max和min #include <iostream> #include<string> #include<fstream> #include<sstream> #include<cstdlib> #include<vector>

我有一个包含许多行和列的值的数据集,如何检查NAN的代码,并用值替换它们,并用“00”填充任何空格

我已经读取了文件,数据被有序地组织到列中,所以我想替换NaN,将行和列的总数相加,然后找到平均值max和min

#include <iostream>
#include<string>
#include<fstream>
#include<sstream>
#include<cstdlib>
#include<vector>

using namespace std;


int const number_of_columns = 25; // declares number of columbs of type int and a constant value

// creating a string array for names of columns
string column_names[number_of_columns] = {
    "Date Time",
    "Year",
    "Month",
    "Day",
    "Time",
    "Data Quality",
    "Temperature",
    "Temp Flag",
    "Dew Point Temp Flag",
    "Rel Hum Flag",
    "Wind Dir Flag",
    "Dew Point",
    "Rel Hum",
    "Wind Dir",
    "Wind Spd",
    "Wind Spd Flag",
    "Visibility",
    "Visibility Flag",
    "Stn Press",
    "Stn Press Flag",
    "Hmdx",
    "Hmdx Flag",
    "Wind Chill",
    "Weather"
};


struct Row
{
    string columns[number_of_columns];
};

int main() {
    // reading data from a string
    ifstream myfile("C:\\Users\\kaileba\\Downloads\\WindEnergy\\data.dat");
    ofstream Output;

    if (myfile.fail())
    {
        cout << "There seems to have been an error!, Good Bye!" << endl;
    }
    else {
        vector<Row> rows;
        string line;
        int row_index = 0;

        //Read rows from file and store in a vector for later use
        while (getline(myfile, line)) {
            stringstream linestream(line);
            string data;

            //create a new row struct and insert into the rows vector
            rows.push_back(Row());
            int col_index = 0;
            while (getline(linestream, data, '\t')) {

                rows[row_index].columns[col_index] = data;
                col_index += 1;

                // if last piece of data increase the row index
                if (data.compare("NA") == 0) {
                    row_index += 1;
                }
            }
        }

        //Open file so we can read to
        Output.open("Results.txt");

        //Get the number of rows in the vector
        int number_of_rows = rows.size();

        //Output all column names
        for (int i = 0; i < number_of_columns; i++)
        {
            if (column_names[i].compare("Weather") == 0) {
                cout << column_names[i];
                Output << column_names[i];
            }
            else {
                cout << column_names[i] << "\t";
                Output << column_names[i] << "\t";
            }
        }

        // Output all rows data
        for (int i = 0; i < number_of_rows; i++)
        {
            string temp_str = "";

            for (int j = 0; j < number_of_columns; j++)
            {
                if (rows[i].columns[j].compare("NA") == 0)
                    temp_str += rows[i].columns[j];
                else
                    temp_str += rows[i].columns[j] + "\t";
            }


        cout << temp_str << "\n";
            Output << temp_str << "\n";
        }
    }

    system("pause");
    return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int const number_of_columns=25;//声明int类型的列数和常量值
//为列名称创建字符串数组
字符串列名称[列数]={
“日期时间”,
“年”,
“月”,
“一天”,
“时间”,
“数据质量”,
“温度”,
“临时标志”,
“露点温度标志”,
“Rel Hum Flag”,
“风向旗”,
“露点”,
“Rel-Hum”,
“风向标”,
“风之Spd”,
“风之Spd旗”,
“可见性”,
“可见性标志”,
“Stn出版社”,
“Stn新闻旗”,
“Hmdx”,
“Hmdx标志”,
“风寒”,
“天气”
};
结构行
{
字符串列[列的数量];
};
int main(){
//从字符串读取数据
ifstream myfile(“C:\\Users\\kaileba\\Downloads\\WindEnergy\\data.dat”);
流量输出;
if(myfile.fail())
{

取消主题:替换
行。使用
Row temp;
向后推(Row());
然后您可以
temp.列[col\u index]=data;
行。在
while(getline…
循环之后向后推(temp);
无需
行索引
如果(data.compare(“NA”)=0)
行。向后推(Row())是为矢量而设计的,它不在乎是在之前还是之后推入
。结果是在之前推入
比在之后推入
会有很多额外的开销。另外,添加构造函数和
运算符[]也会带来很大的可读性好处
。另一方面,由于空间位置不佳,在向量中使用向量存在严重的性能缺陷,因此请恕我直言。我想让它简单得多,但建议使用向量,因为我不知道行的数量……大约有300多行,大约25列