C++ input a line of unknown number of integers

  This situation is easy to encounter in the online written test: 输入一行整数,个数未知,整数之间用空格间隔, in addition to string segmentation and extraction, the following simple methods can be used:

int main()
{
    vector<int> inputs;
    int tmp;  

    cin >> tmp;
    inputs.push_back(tmp);
    while (cin.get() != '\n')   { 
        cin >> tmp; 
        inputs.push_back(tmp);
    }   

    return 0;
}

Reference:
https://blog.csdn.net/lc013/article/details/77603310

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324892634&siteId=291194637