开发者

getting input numbers from the user

How can I get input from the user if they are to separate their inputs by whitespace (e.g. 1 2 3 4 5) and I want to put it in an array? Thanks.

Hmmmm. I see most of the responses are using a vector which I guess I'll have to do research on. I thought there would be a more simpler, yet possibly messier response since we haven't cov开发者_C百科ered vectors like using sscanf or something. Thanks for the inputs.


#include <vector>
#include <iostream>
using namespace std;

int main()
{
   vector<int> num;
   int t;
   while (cin >> t) {
     num.push_back(t);
   }
}


Alternatively, and a more generic form:

#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
using namespace std;

int main()
{
   vector<int> num;

   copy(istream_iterator<int>(cin), istream_iterator<int>(), back_inserter(num));
}


#include <iostream>
#include <iterator>
#include <vector>

   std::istream_iterator< int > iterBegin( std::cin ), iterEnd;
   std::vector< int > vctUserInput( iterBegin, iterEnd );
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜