使用ints C ++获取getline

时间:2013-10-30 15:44:48

标签: c++ for-loop

我有一个文件

0 3 2

1 2 3

4 5 6

6 8 1

如果每行的第一个数字是行,则第二个数字是列,第三个数字是该行,列中包含的数据。这将是一个给定的[8] [8]数组,所以我已经将所有内容初始化为0,但是如何存储这些数据值?例如,我想要[0] [3] = 2和[1] [2] = 3.我想跟踪我找到行,列和数据值的行。那么,我怎样才能正确地将这些值插入到我的二维数组中呢?

int rowcol[8][8];
    for (int i=0; i < 9; i++)
        for (int j=0; j < 9; j++)
        {
            rowcol[i][j] =0;
        }

 ifstream myfile;
int nums;
myfile.open(text.c_str());
while (!myfile.eof()) {
    myfile >> nums;
    numbers.push_back(nums);
}
for (int i=0; i < numbers.size(); i++)
{

   //Not sure what the best approach here would be and I'm not even sure if I should have done a vector...

}

4 个答案:

答案 0 :(得分:2)

为什么你读入数字向量,为什么不在读取每一行时直接写入rowcol?

// Check myfile and not only myfile.eof()
int row, column, value;
while(myfile >> row >> column >> value) {
    rowcol[row][column] = value;
}

此代码不会检查一行中只有3个数字,具体取决于您为此添加检查所需的要求。

答案 1 :(得分:0)

只需读取行,col值并更新行col:

int rowcol[8][8];

for (int i=0; i < 9; i++)
    for (int j=0; j < 9; j++)
    {
        rowcol[i][j] =0;
    }

myfile.open(text.c_str());

while (!myfile.eof()) {
    int row, col, val;
    myfile >> row >> col >> val;
    rowcol[row][col] = val;
}

更好的解决方案可以是一个表示行数的数字:

// file struct
2
0 0 1
0 1 2

template< typename T >
inline T read( std::istream& is )
{
    T res;
    is >> res;
    return res;
}     

int rowcol[8][8];

for (int i=0; i < 9; i++)
    for (int j=0; j < 9; j++)
    {
        rowcol[i][j] =0;
    }

myfile.open(text.c_str());

const size_t COUNT = read<int>( myfile );

for ( int i = 0; i < COUNT; ++i )

    int row = read<int>( myfile );
    int col = read<int>( myfile );
    int val = read<int>( myfile );

    rowcol[row][col] = val;
}

答案 2 :(得分:0)

您的代码无效。如果你定义了一个大小等于8的数组,那么在循环中你应该使用条件i&lt; 8不是我&lt; 9,你写的

int rowcol[8][8];
    for (int i=0; i < 9; i++)
        for (int j=0; j < 9; j++)
        {
            rowcol[i][j] =0;
        }

此外,在定义数组时可以进行这种初始化

int rowcol[8][8] = {};

对于从文件中读取记录的代码,您应检查每行是否包含正好三个数字,前两个数字对于数组的索引具有可接受的值。

答案 3 :(得分:0)

你可以使用矢量

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

using namespace std;


vector<int> split(string line,char delm)
{
    vector<int> tokens;
    string num="";
    for(int i=0;i<line.length();i++)
    {
        char c = line[i];
        if(c == delm)
        {
            tokens.push_back(atoi(num.c_str()));
            num="";
        }else
        {
            num+=c;
        }
    }
    return tokens;
}

string text="file.txt";


int main()
{
    string line = "";
    ifstream infile;

    infile.open(text.c_str());
    vector<vector<int>> mydata;

    while (!infile.eof())
    {
        getline(infile, line);
        mydata.push_back(split(line,' '));
    }
    infile.close();

    system("pause");
    return 0;
}

如果要将其转换为数组

int rowcol[8][8];
    for (int i=0; i < mydata.size(); i++)
    {
        vector<int> d = mydata[i];
        for (int j=0; j < d.size(); j++)
        {
            rowcol[i][j] =d[j];
        }
    }
相关问题