Wednesday, May 11, 2011

STL stack example c++ objc

Here's a simple STL stack example code in c++. It just reverses a string using a STL stack object. [STL stack example]

void reverse(string & x)
//Afterwards x will have the same elements in the opposite order.
{
stack<char> s;
const int n = x.length();
//Put characters from x onto the stack
for(int i=0; i<n; ++i)
s.push(x[i]);
//take characters off of stack and put them back into x
for(int i=0; !s.empty(); ++i, s.pop())
x[i]=s.top();
}