empty [C++ Reference]
Keywords: stl,stack,empty
Date: 2009-07-16T14:13:39-0700

## empty

Syntax:

        #include <stack>
        bool empty() const;

The empty() function returns true if the stack has no elements, false otherwise.

For example, the following code uses empty() as the stopping condition on a while loop to clear a stack and display its contents in reverse order:

       stack<int> s;
       for( int i = 0; i < 5; i++ ) {
         s.push(i);
       }
       while( !s.empty() ) {
         cout << s.top() << endl;
         s.pop();
       }

Related Topics: size