Z
zr
Hi,
i need to read a text file which contains a list of items, all of type
ItemType, separated by whitespace and newlines. For each line, there
will be a vector<ItemType> object that will store the integers read in
that line. There will be a single vector<vector<ItemType>> object that
will stores all of the vector<ItemType> objects mentioned in the
previous sentence.
I wrote a quick implementation and it seems to be working, but i would
like to hear some opinions on what may be incorrect or may be
improved, especially exception and runtime error handling. Assume that
the required operator>> in code line 13 is defined.
TIA
1 template <class ItemType>
2 void collect(istream& source, vector<vector<ItemType>>& db) {
3 while (source.good()) {
4 string s;
5 getline(source, s, '\n');
6 istringstream iss(s);
7 if (iss.bad() || 0==s.size())
8 break;
9 vector<ItemType> t;
10
11 while (iss.good()) {
12 ItemType item;
13 iss >> item;
14 t.push_back(item);
15 }
16 db.push_back(t);
17 }
18}
i need to read a text file which contains a list of items, all of type
ItemType, separated by whitespace and newlines. For each line, there
will be a vector<ItemType> object that will store the integers read in
that line. There will be a single vector<vector<ItemType>> object that
will stores all of the vector<ItemType> objects mentioned in the
previous sentence.
I wrote a quick implementation and it seems to be working, but i would
like to hear some opinions on what may be incorrect or may be
improved, especially exception and runtime error handling. Assume that
the required operator>> in code line 13 is defined.
TIA
1 template <class ItemType>
2 void collect(istream& source, vector<vector<ItemType>>& db) {
3 while (source.good()) {
4 string s;
5 getline(source, s, '\n');
6 istringstream iss(s);
7 if (iss.bad() || 0==s.size())
8 break;
9 vector<ItemType> t;
10
11 while (iss.good()) {
12 ItemType item;
13 iss >> item;
14 t.push_back(item);
15 }
16 db.push_back(t);
17 }
18}