+ 1
How do you parse a file into a 2D vector?
How do you read in any text file into a 2D vector? This text files has characters in it.
3 ответов
+ 2
Maybe you wanted to do this? :
fstream fin; char ch;
string name; //File Name.
vector<vector<char>> vec;
// 2D Vector.
vector<char> temp;
// Temporary vector to be pushed
// into vec, since its a vector of vectors.
fin.open(name.c_str(),ios::in);
// Assume name as an arbitary file.
while(fin)
{
ch = fin.get();
if(ch!='\n') temp.push_back(ch);
else { vec.push_back(temp);
temp.clear(); }
}
fin.close();
for(vector<char> v:vec)
// Print the 2D Vector contents.
{
for(char c:v) cout<<c;
cout<<endl;
}
+ 2
Which language? What data is in the file? Do you want to use a parser or your own code?
Note: 2D vectors are called matrices
+ 2
In c++