TheDeveloperBlog.com

Home | Contact Us

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

C++ Stack emplace() Function

C++ Stack emplace() 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 emplace() Function

C++ Stack emplace() function adds a new element at the top of the stack above the current top element. Now, we have a stack with already existing elements, and we wish to insert or push a new element in the stack, for doing that we use this function.

Syntax

template <class... Args> void emplace (Args&&... args);

Parameters

args:The parameter forwards the argument for the construction of new element. That is the element specified by args is inserted in the stack over the current top element. The newly inserted element now becomes the top element and all the push and pop operations are performed on it.

Return value

The function is used only for the addition of new elements and does not return any value. Hence the return type of the function is void.

Example 1

//The program illustrates the use of emplace function by adding two simple strings at the top of the stack and printing them.

#include<iostream>
#include<stack>
#include<string>
int main()
{
	std: : stack<std: : string> newstack;
	newstack.emplace (?I am the first line?);
	newstack.emplace (?I am the second one?);
	std: :cout << ?Contents of newstack: \n?;
	while (!newstack.empty () )
	{
		std: :cout << newstack.top () << ?\n?;
		newstack.pop ();
	}
	return 0;
}

Output:

Contents of newstack:
I am the second one
I am the first line

Example 2

//The program illustrates the use of emplace function by inserting the table of 11 on the and then respectively printing it.

#include<iostream>
#include<stack>
#include<string>
int main()
{
	std: : stack<std: : string> newstack;
	newstack.emplace (?11?);
	newstack.emplace (?22?);
	newstack.emplace (?33?);
	newstack.emplace (?44?);
	newstack.emplace (?55?);
	newstack.emplace (?66?);
	newstack.emplace (?77?);
	newstack.emplace (?88?);
	newstack.emplace (?99?);
	newstack.emplace (?121?);
	std: :cout << ?Contents of newstack: \n?;
	 std: :cout <<?Table of 11?;

	while (!newstack.empty () )
	{
		std: :cout << newstack.top () << ?\n?;
		newstack.pop ();
	}
	return 0;
}

Output:

Contents of newstack: 
Table of 11121
99
88
77
66
55
44
33
22
11

Example 3

//The program illustrates the use of emplace function by adding two simple strings at the top of the stack and printing them.

#include<iostream>
#include<stack>
#include<string>
int main()
{
	std::stack<std::string> newstack;
	newstack.emplace ("We are here to see the application use of emplace function in stacks");
	newstack.emplace ("The function adds new elements are the top of the stack");
	while (!newstack.empty () )
	{
		std::cout << newstack.top () << "\n";
		newstack.pop ();
	}
	return 0;
}

Output:

The function adds new elements are the top of the stack         
We are here to see the application use of emplace function in stacks 

Complexity

One call is made to the emplace_back. The function is used for inserting a new element which is done by making a single call.

Data races

All the elements present in the stack are modified. Since the element is added to the top hence respective positions of all the other elements are also changed.

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