C++ stack push() Function
Example
Add elements to a stack:
stack<string> cars;
cars.push("Volvo");
cout << cars.top() << "\n";
cars.push("BMW");
cout << cars.top();
Definition and Usage
The push() function adds an element to the top of a stack.
The new element becomes the top element. In the example, top() always returns the element that was added last.
The push function is defined in the <stack> header file.
Syntax
stack.push(<type> value);
<type> refers to the type of the data that the stack contains.
Parameter Values
| Parameter | Description |
|---|---|
| value | Required. The value of the element to add. |
Technical Details
| Returns: | Nothing (void). |
|---|
Related Pages
Read more about stacks in our Stacks Tutorial.