The C++ function std::stack::push() inserts new element at the top of the stack and increases size of stack by one.
DeclarationFollowing is the declaration for std::stack::push() function form std::stack header.
C++98void push (const value_type& val);C++11
void push (const value_type& val);Parameters
val − Value to be assigned to the newly inserted element.
Return valueNone.
ExceptionsDepends upon underlying container.
Time complexityConstant i.e. O(1)
ExampleThe following example shows the usage of std::stack::push() function.
#include <iostream> #include <stack> using namespace std; int main(void) { stack<int> s; for (int i = 0; i < 5; ++i) s.push(i + 1); cout << "Stack contents are" << endl; while (!s.empty()) { cout << s.top() << endl; s.pop(); } return 0; }
Let us compile and run the above program, this will produce the following result −
Stack contents are 5 4 3 2 1
stack.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4