//: C07:Stack2.cpp // From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison. // (c) 1995-2004 MindView, Inc. All Rights Reserved. // See source code use permissions stated in the file 'License.txt', // distributed with the code package available at www.MindView.net. // Converting a list to a stack. #include #include #include #include #include #include using namespace std; // Expects a stack: template void stackOut(Stk& s, ostream& os = cout) { while(!s.empty()) { os << s.top() << "\n"; s.pop(); } } class Line { string line; // Without leading spaces size_t lspaces; // Number of leading spaces public: Line(string s) : line(s) { lspaces = line.find_first_not_of(' '); if(lspaces == string::npos) lspaces = 0; line = line.substr(lspaces); } friend ostream& operator<<(ostream& os, const Line& l) { for(size_t i = 0; i < l.lspaces; i++) os << ' '; return os << l.line; } // Other functions here... }; int main() { ifstream in("Stack2.cpp"); list lines; // Read file and store lines in the list: string s; while(getline(in, s)) lines.push_front(s); // Turn the list into a stack for printing: stack > stk(lines); stackOut(stk); } ///:~