我正在尝试从标有 {1...9} 或 X 的文件中读取输入。我需要将这些值分开并将它们正确地存储在一个 vector 中。我正在使用“sstringstream”来帮助我这样做:
void myclass::Initialize(ifstream &file_name)
{
string input;
int value;
//Initialize the values in the matrix from the values given
//in the input file, replace x with a 0
for(int i = 0; i < 9; i++)
{
for(int j = 0; j < 9; j++)
{
//Read the input from the file and determine
//if the entry is an "int" or "x"
file_name >> input;
cout << input << endl;
istringstream(input);
if(input >> value) //PROBLEM HERE!!
{
Matrix[i][j] = value;
cout << "Debug: Check for values in matrix: " << Matrix[i][j] << endl;
}
else
Matrix[i][j] = 0;
}
}
cout << "The values in Matrix after initialization: " << endl;
Print_result();
}
问题出现在if语句中,当“input”中有一个整数时,它不执行if语句。我不确定为什么它不起作用。
最佳答案
您实际上并没有使用 istringstream。我想你正在寻找类似的东西,
..
istringstream is(input);
if (is >> value)
{
...
其中 'is' 是从字符串“input”创建的 istringstream。
关于c++ - Isstringstream 不适用于整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12611512/