C++ Extract int from string using stringstream

后端 未结 3 1499
無奈伤痛
無奈伤痛 2020-12-10 09:50

I am trying to write a short line that gets a string using getline and checks it for an int using stringstream. I am having trouble with how to check if the part of the stri

3条回答
  •  Happy的楠姐
    2020-12-10 10:18

    You could make of the validity of stringstream to int conversion:

    int main() {
    
    std::stringstream ss;
    std::string input = "a b c 4 e";
    ss << input;
    int found;
    std::string temp;
    
    while(std::getline(ss, temp,' ')) {
        if(std::stringstream(temp)>>found)
        {
            std::cout<

提交回复
热议问题