C++: Containers: Example 1 Using a Stack
One of three contrasting examples of solving the problem with increasingly
powerful tools:
using arrays, then
vectors, then
stacks.
// Read words and print them in reverse order.
// Variation using stack and string.
// Fred Swartz 2001-11-08, 2001-12-04
#include <iostream>
#include <stack>
#include <string>
using namespace std;
int main() {
stack<string> allwords; // stack of all words
string word; // input buffer for words.
// read words/tokens from input stream
while (cin >> word) {
allwords.push(word);
}
cout << "Number of words = " << allwords.size() << endl;
// write out all the words in reverse order.
while (!allwords.empty()) {
cout << allwords.top() << endl;
allwords.pop(); // remove top element
}
return 0;
}//end main