Read a sequence of ints from cin and store them in a vector

前端 未结 4 1592
粉色の甜心
粉色の甜心 2021-01-26 11:41

This is what I have done to read integers with std::cin and store them in a vector:

int number; 
vectorivec;

while (cin>>number)
{         


        
4条回答
  •  天涯浪人
    2021-01-26 12:38

    Please find a simple solution to your problem, let me know if you see any issue with this solution.

    #include 
    #include 
    using namespace std;
    int main()
    {
        vector va;
        int x;
        while ( cin >> x ) {
              va.push_back(x);
              if ( cin.get() == '\n' ) break;
        }
        //Vector output
        for ( int i = 0; i < va.size(); i++ ) {
            cout << va[i] <<" ";
        }
        return 0;
    }
    

提交回复
热议问题