c++ - Isstringstream 不适用于整数

标签 c++ string file vector io

我正在尝试从标有 {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/

相关文章:

android - 如何从Android上的SD卡读取选定的文本文件

file - Angular 2 将图像编码为 base64

c++ - 如何在读取文件时打印数组

c++ - 两组的有效交集

c++ - 在库的文件名中使用加号 ( '+' ) 是不是很糟糕?

c++ - 从元组函数一次存储 2 个变量

c++ - 从一个数组到另一个数组的选择性浅拷贝

string - Swift 如何检查多个字符串条件的变量

java - 按特定字符串值对字符串数组进行排序

java - 如何在 Java 中比较字符串?