C++ stack size() Function
Example
Get the number of elements in a stack:
stack<string> cars;
cars.push("Volvo");
cars.push("BMW");
cars.push("Ford");
cars.push("Mazda");
cout << cars.size();
Definition and Usage
The size() function returns the number of elements in a stack.
The size function is defined in the <stack> header file.
Syntax
stack.size();
Parameter Values
None.
Technical Details
| Returns: | The number of elements in the stack (a non-negative integer). |
|---|
Related Pages
Read more about stacks in our Stacks Tutorial.