The most usual way this is accomplished is to use istream_iterator 's .
Scott meyers wrote an article in his more effective c++ book called " be alert for c++'s most vexing parse" that shows how to do this and the pitfalls of getting it wrong.
std:: omitted for brevity.
Code:
ifstream file("file.dat");
vector<int> bob( (istream_iterator<int>(file)), istream_iterator<int>());
This is technically correct.
this below isn't....
Code:
ifstream file("file.dat");
vector<int> bob(istream_iterator<int>(file),istream_iterator<int>());
Can you work out why the first should work according to the c++ standard and yet the second will not. To be portable between compilers tho it is often better coded like this...
Code:
ifstream file("file.dat");
istream_iterator<int> filebegin(file);
istream_iterator<int> fileend;
vector<int> bob(filebegin,fileend);
Of course this just illustrates range construction from a file. If the vector is precreated you can use vector::insert() instead.