I have a read function which takes numbers from a text file and stored them into a data structure. I have made this function.
void VectorIntStorage::read(ifstream &in)
{
if(in.is_open())
{
for (int i = 0; in && i < n; ++ i)
{
in >> vectorStorage<i>;
}
}
}
I开发者_开发技巧 am trying to add them into a vector structure, is this code correct??
No, it isn't. The canonical way would be:
vector <int> v;
int n;
while( f >> n ) {
v.push_back( n );
}
where f is an ifstream.
No, if you write you code this way the compilation will fail. Maybe you can allocate enough space for the vector and then store the date the ifstream read.
vector<int> v(MAX_SIZE);
int iIndex = 0;
while((iIndex < v.size()) && (in >> v[iIndex]))
{
++iIndex;
}
精彩评论