#include // for exit #include #include #include #include #include #include // according to C++ standard using namespace std; class Line { public: Line(); Line(istream& input); int size() const; string getWord(int n) const; const string& operator[](int n) const; private: vector myWords; }; Line::Line() { // nothing to do } Line::Line(istream& input) // post: line read from input, stored in *this { string line,word; getline(input,line); // istringstream in(line); // this is up-to-date version istrstream in(line.c_str()); while (in >> word) { myWords.push_back(word); } } int Line::size() const // post: returns # words in a line { return myWords.size(); } string Line::getWord(int n) const // pre: 0 <= n < size() // post: return n-th word { return myWords[n]; } const string& Line::operator[](int n) const // pre: 0 <= n < size() // post: return n-th word { return myWords[n]; } class Lines { public: Lines(istream& input); int size() const; Line getLine(int n) const; private: vector myLines; }; Lines::Lines(istream& input) // post: all lines from input read and stored { while (input) { myLines.push_back(Line(input)); } } int Lines::size() const // post: returns # lines { return myLines.size(); } Line Lines::getLine(int n) const // pre: 0 <= n < size() // post: returns # lines { return myLines[n]; } int main(int argc, char * argv[]) { if (argc == 1) { cerr << "usage: " << argv[0] << " filename " << endl; exit(1); } ifstream input(argv[1]); Lines all(input); int j,k; for(j=0; j < all.size(); j++) { Line one = all.getLine(j); for(k=0; k < one.size(); k++) { cout << j << "." << k << "\t" << one.getWord(k) << endl; // cout << j << "." << k << "\t" << one[k] << endl; } } return 0; }