Microsoft KB Archive/158039

= INFO: STL Sample for the stack::size Function =

Article ID: 158039

Article Last Modified on 12/2/2003

-

APPLIES TO

 The Standard C++ Library, when used with:  Microsoft Visual C++ 4.2 Enterprise Edition

 Microsoft Visual C++ 5.0 Enterprise Edition

 Microsoft Visual C++ 6.0 Enterprise Edition

 Microsoft Visual C++ 4.2 Professional Edition</li></ul>

 Microsoft Visual C++ 5.0 Professional Edition</li></ul>

 Microsoft Visual C++ 6.0 Professional Edition</li></ul>

 Microsoft Visual C++ 6.0 Standard Edition</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q158039

<div class="summary_section">

SUMMARY
The sample code below illustrates how to use the stack::size STL function in Visual C++.

<div class="moreinformation_section">

Prototype
template<class _TYPE, class _C, class _A> size_type stack::size const; NOTE: The class/parameter names in the prototype may not match the version in the header file. Some have been modified to improve readability.

Description
The stack::size function returns the number of elements in the stack. It is okay to call this function with an empty stack; it will return a value of 0.

Sample Code
////////////////////////////////////////////////////////////////////// // // Compile options needed: /GX // // StackSize.cpp : Illustrates how to use the size function to determine //                the number of elements on the stack. // // Functions: // //   size :  returns the number of elements in the stack. // // Written by Derek Jamison // of Microsoft Product Support Services, // Copyright (c) 1996 Microsoft Corporation. All rights reserved. //////////////////////////////////////////////////////////////////////


 * 1) pragma warning(disable:4786)

using namespace std;
 * 1) include
 * 2) include
 * 3) include

using namespace std; // std c++ libs implemented in std #endif
 * 1) if _MSC_VER > 1020  // if VC++ version is > 4.2

typedef stack<string, deque<string, allocator >,

allocator > STACK_STRING;

void main

{

STACK_STRING stack1;

// Check the size of an empty stack. Should return 0. cout << "stack1.size equals " << stack1.size << endl;

// Add item "Hello" to Stack1. cout << "stack1.push('Hello')" << endl; stack1.push("Hello");

// Add item "This is the second element" to Stack1. cout << "stack1.push('This is the second element')" << endl; stack1.push("This is the second element");

// Check the size of Stack1. Should return 2. cout << "stack1.size equals " << stack1.size << endl << endl;

// Add item "Third element" to Stack1. cout << "stack1.push('Third element')" << endl; stack1.push("Third element");

// Check the size of Stack1. Should return 3. cout << "stack1.size equals " << stack1.size << endl << endl;

// Pop "Third element". cout << "stack1.pop" << endl; stack1.pop;

// Pop "This is the second element". cout << "stack1.pop" << endl; stack1.pop;

// Check the size of Stack1 again. Should return 1. cout << "stack1.size equals " << stack1.size << endl << endl;

// Pop "Hello". cout << "stack1.pop" << endl; stack1.pop;

// Check the size of Stack1. Should return 0. cout << "stack1.size equals " << stack1.size << endl << endl;

} Program Output is: <pre class="fixed_text">stack1.size equals 0

stack1.push('Hello') stack1.push('This is the second element') stack1.size equals 2

stack1.push('Third element') stack1.size equals 3

stack1.pop stack1.pop stack1.size equals 1

stack1.pop stack1.size equals 0

<div class="references_section">