How to read space separated numbers from console?

后端 未结 5 1777
无人共我
无人共我 2021-01-12 03:13

I\'m trying to do a simple task of reading space separated numbers from console into a vector, but I\'m not getting how to do this properly.

5条回答
  •  时光取名叫无心
    2021-01-12 03:44

    Use a getline combined with an istringstream to extract the numbers.

    std::string input;
    getline(cin, input);
    std::istringstream iss(input);
    int temp;
    while(iss >> temp)
    {
       yourvector.push_back(temp);
    }
    

提交回复
热议问题