TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C++ Stack top() Function

C++ Stack top() Function with Examples on tutorial for beginners and professionals, constructor, emplace(), empty(), pop(), push(), size(), top(), object and class, exception, static, structs, inheritance, aggregation etc.

<< Back to CPP

C++ stack top() function

C++ Stack top() function returns the value of the top element in the stack. The top element is the one which was recently added on the stack. The last added element is the top element. Of all the elements that are present in a stack the top element stands out and is more significant as all the major operations on the stack are performed at the top element. Be it push, pop or anything all the operations are done at the top most position.

Syntax

value_type& top();
const value_type& top() const;

Parameters

The function is used only for returning the value of the top element and hence takes nothing as parameters. The return type of the function is based on the value type of the stack.

Return value

The function returns the top element of the stack.

Example 1

//The program illustrates the use of top() function in stack to retrieve the value of top most element.

#include <iostream>
#include <stack>
int main()
{
	std::stack<int> newstack;
	newstack.push(24);
	newstack.push(80);
	newstack.top () +=20;
	std::cout <<"newstack.top() is modified to" <<newstack.top ();
	return 0;
}

Output:

newstack.top() is modified to 100

Example 2

//The program illustrates the use of top() function in stack to retrieve the value of top most element.

#include <iostream>
#include <stack>
using namespace std;
int main()
{
	int result = 0;
	stack<int> newstack;
	newstack.push(2);
	newstack.push(7);
	newstack.push(4);
	newstack.push(5);
	newstack.push(3);
	while(!newstack.empty() )
	{
		result = result + newstack.top();
		newstack.pop();
	}
	cout<<result;
	return 0;
}

Output:

21

Example 3

//The program illustrates the use of top() function in stack to retrieve the value of top most element.

#include <iostream>      
#include <stack>          
int main ()
{
  std::stack<int> newstack;
  newstack.push(9);
  newstack.push(14);
   std::cout << "newstack.top() is " << newstack.top() << '\n';
  return 0;
}

Output:

newstack.top() is 14

Complexity

The complexity of the function is constant. The function only retrieves the value of the top element and does not take any extra time or space.

Data races

The function accesses the container, and retrieves the element which was last inserted. The top most element of the stack is given.

Exception Safety

Guarantee as equivalent to the operations that are performed on the underlying container object is provided.

Next TopicC++ Stack




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf