C++ getline与ints C++;
我有一份档案 032 1 2 3 4 5 6 681 其中,每行的第一个数字是行,第二个数字是列,第三个数字是该行、列中包含的数据。这将是一个给定的[8][8]数组,因此我已将所有内容初始化为0,但如何存储这些数据值?例如,我希望[0][3]=2和[1][2]=3。我希望跟踪我在其中找到行、列和数据值的行。那么,如何将这些值正确地插入到我的二维数组中呢C++ getline与ints C++;,c++,for-loop,C++,For Loop,我有一份档案 032 1 2 3 4 5 6 681 其中,每行的第一个数字是行,第二个数字是列,第三个数字是该行、列中包含的数据。这将是一个给定的[8][8]数组,因此我已将所有内容初始化为0,但如何存储这些数据值?例如,我希望[0][3]=2和[1][2]=3。我希望跟踪我在其中找到行、列和数据值的行。那么,如何将这些值正确地插入到我的二维数组中呢 int rowcol[8][8]; for (int i=0; i < 9; i++) for (int j=0;
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...
}
int-rowcol[8][8];
对于(int i=0;i<9;i++)
对于(int j=0;j<9;j++)
{
rowcol[i][j]=0;
}
ifstreammyfile;
int nums;
myfile.open(text.c_str());
而(!myfile.eof()){
myfile>>nums;
数字。推回(nums);
}
对于(int i=0;i
为什么要读取数字向量,为什么不在读取每一行时直接写入rowcol
// Check myfile and not only myfile.eof()
int row, column, value;
while(myfile >> row >> column >> value) {
rowcol[row][column] = value;
}
此代码不会检查一行中是否只有3个数字,这取决于您可能需要添加检查的要求。只需读取行、列val并更新thw rowcol:
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;
}
int-rowcol[8][8];
对于(int i=0;i<9;i++)
对于(int j=0;j<9;j++)
{
rowcol[i][j]=0;
}
myfile.open(text.c_str());
而(!myfile.eof()){
int row、col、val;
myfile>>行>>列>>值;
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 0 1
0 1 2
模板
内联T读(标准::istream&is)
{
T res;
is>>res;
返回res;
}
int rowcol[8][8];
对于(int i=0;i<9;i++)
对于(int j=0;j<9;j++)
{
rowcol[i][j]=0;
}
myfile.open(text.c_str());
const size\u t COUNT=读取(myfile);
对于(int i=0;i
您的代码无效。如果定义了一个数组,其一维的大小等于8,那么在循环中应该使用条件i<8,而不是像您所写的那样使用条件i<9
int rowcol[8][8];
for (int i=0; i < 9; i++)
for (int j=0; j < 9; j++)
{
rowcol[i][j] =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;
}
#包括
#包括
#包括
#包括
使用名称空间std;
向量拆分(字符串行、字符增量)
{
向量标记;
字符串num=“”;
对于(inti=0;类似intr,c,val;而(myfile>>r>>c>>val)rowcol[r][c]=val;
#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];
}
}