简体   繁体   English

向量将CSV列数据添加到向量行中,同时将CSV文件的数据传输到c ++向量中

[英]vector adds CSV column data into vector rows while transfering CSV file's data into c++ vector

I am trying to add CSV file into a 2D array vector ie: vector of vector. 我正在尝试将CS​​V文件添加到2D数组向量中,即:向量的向量。 The following program works well but there is a small problem, 以下程序运行良好,但存在一个小问题,

For example to add the following CSV data: 例如,添加以下CSV数据:

1.4,23.44,24.4,3.0
2.3,4.3,44.5,6.6
3.4,33.2,5.4,3.65

i have used the following code: 我使用了以下代码:

void Table::addTableData(string filename)
    vector<vector<float> > vec;

        ifstream file(filename.c_str());


            bool first = false;
            if (file)
            {
                string line;

                while (getline(file,line)) //reading the data line by line
                {
                    istringstream split(line);
                    float value;
                    int col = 0;
                    char sep;

                while (split >> value) //
                {
                    if(!first)
                    {
                        // Each new value read on line 1 should create a new inner vector
                        vec.push_back(std::vector<float>());
                    }

                    vec[col].push_back(value);
                    ++col;

                    // read past the separator
                    split>>sep;
                }

                // Finished reading line 1 and creating as many inner
                // vectors as required
                first = true;
            }

However, The above code executes and adds data into the vector, but rather than adding each value in the first line in inner vector it adds the column as a row in the inner vector. 但是,上面的代码执行并将数据添加到向量中,但不是在内部向量的第一行中添加每个值,而是在内部向量中将列添加为一行。 I mean to say the rows and column in the CSV file becomes column and rows in the vector respectively. 我的意思是说CSV文件中的行和列分别成为向量中的列和行。 If I do a cout then I get the following result 如果我进行提示,则会得到以下结果

1.4 2.3 3.4
23.44 4.3 33.2
24.4 44.5 5.4 
3.0 6.6 3.65   

Therefore. 因此。 the row and column is reversed, How do I turn things around. 行和列颠倒了,我该如何扭转。

Thank you for looking at this probelm. 感谢您查看此探针。

Just add each row into a vector, and then push it back, instead of adding to each vector each time: 只需将每行添加到向量中,然后将其推回,而不是每次都添加到每个向量中:

while (getline(file,line)) //reading the data line by line
{
    std::vector<float> nextRow;

    // etc.
    while (split >> value)
    {
        nextRow.push_back(value);
        split >> sep;
    }

    vec.push_back(nextRow);
}
void Table::addTableData(string filename)

{ vector > vec; {vector> vec;

ifstream file(filename.c_str());

bool first = false;
if (file)
{
    string line;
    int col = 0;
    while (getline(file,line)) //reading the data line by line
    {
        istringstream split(line);
        float value;

        char sep;
        vec.push_back(std::vector<float>());
        while (split >> value) //
        {
            /*
            if(!first)
            {
                // Each new value read on line 1 should create a new inner vector
                vec.push_back(std::vector<float>());
            }
            */

            vec[col].push_back(value);

            /* ++col; */

            // read past the separator
            split>>sep;
        }

        // Finished reading line 1 and creating as many inner
        // vectors as required
        /* first = true; */
        ++col;
    }
}

} }

this code may work well 该代码可能很好用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM