A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.tutorialspoint.com/cpp_standard_library/cpp_stack_push_copy.htm below:

C++ Stack Push Copy

C++ Stack Library - push() Function Description

The C++ function std::stack::push() inserts new element at the top of the stack and increases size of stack by one.

Declaration

Following is the declaration for std::stack::push() function form std::stack header.

C++98
void 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 value

None.

Exceptions

Depends upon underlying container.

Time complexity

Constant i.e. O(1)

Example

The 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