There are two steps. Some of these other answers are assuming you want to read the whole file into a single vector; I'm assuming you want each separate line to be in its own vector. This is not the complete code, but it should give you the right idea.
First, you need to read through the file line-by-line. For each line, read it into a std::string using std::getline, where the file itself is opened using an ifstream. Something like this:
ifstream in_file( "myfile.txt" );
string s;
while( in_file.good() )
{
getline( in_file, s );
if( !in_file.fail() )
{
// See below for processing each line...
}
}
Given that you have read in a line, you should load it into a std::stringstream, then read the integers from that stringstream.
vector< int > result;
stringstream ss( s );
while( ss.good() )
{
int x;
ss >> x;
if( !ss.fail() )
result.push_back( x );
}
This part goes in the inner loop above. At the end of this block of code, the result
vector will contain all the integers from the line stored in s
.
By the way, there are some nice tricks with stream iterators (see the other answers) that can make this code a bit more brief. However, I like this style--it's fairly easy to adapt to more complex line formatting later on, and it's easier to add error checking.