Use istream_iterator with the copy algorithm
#include <iostream>
#include <iterator>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
int i;
vector<int> v(5);
cout << "Enter 5 integers: \n";
istream_iterator<int> int_itr(cin);
copy(int_itr, istream_iterator<int>(), v.begin());
cout << "Here are the values you entered: ";
for(i = 0; i <v.size(); i++) cout << v[ i ] << " ";
return 0;
}
Related examples in the same category