String 如何在不使用getline函数的情况下存储字符串中的整数。C++

String 如何在不使用getline函数的情况下存储字符串中的整数。C++,string,input,getline,String,Input,Getline,很抱歉,我一直在到处寻找从这组字符串中提取整数的方法: {(1,2),(1,5),(2,1),(2,3),(3,2),(3,4),(4,3),(4,5),(5,1),(5,4)} 我真的不需要做家庭作业,如果你能给我举个例子,我会很感激的。 提前谢谢你 如果您只想从这样的行中访问整数,一种方法是在可以的时候继续读取整数 例如,如果由于某种原因,您发现整数读取失败,因为输入流中有一个{,请跳过该单个字符并继续 这方面的示例代码是: #include <iostream> int m

很抱歉,我一直在到处寻找从这组字符串中提取整数的方法:

{(1,2),(1,5),(2,1),(2,3),(3,2),(3,4),(4,3),(4,5),(5,1),(5,4)}
我真的不需要做家庭作业,如果你能给我举个例子,我会很感激的。
提前谢谢你

如果您只想从这样的行中访问整数,一种方法是在可以的时候继续读取整数

例如,如果由于某种原因,您发现整数读取失败,因为输入流中有一个{,请跳过该单个字符并继续

这方面的示例代码是:

#include <iostream>

int main() {
    int intVal;                              // for getting int
    char charVal;                            // for skipping chars
    while (true) {
        while (! (std::cin >> intVal)) {     // while no integer available
            std::cin.clear();                // clear fail bit and
            if (! (std::cin >> charVal)) {   //   skip the offending char.
                return 0;                    // if no char left, end of file.
            }
        }
        std::cout << intVal << '\n';         // print int and carry on
    }
    return 0;
}

这真是太棒了!!我正在玩它来适应我的代码,因为我必须翻译成矩阵!!谢谢,非常感谢,我不知道你可以像!std::cin>>intVal这样的东西来测试它是整数还是字符!再次感谢!!
pax> echo '{(314159,271828),(42,-1)}' | ./testprog
314159
271828
42
-1