TheDeveloperBlog.com

Home | Contact Us

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

C++ Queue empty() Function

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

<< Back to CPP

C++ Queue empty() Function

C++ Queue empty() function is used for testing whether the container is empty or not. Sometimes before actually starting the work with the individual elements of the containers, it is more feasible to look up if the container is empty, so this function finds its usage in such cases.

Syntax

bool empty() const;

Parameters

There are no parameters. The function is only used to test for the emptiness of the container and hence takes no parameter.

Return value

If the container under reference is empty, then the method returns 'true' else returns 'false'.

Example 1

#include <iostream>
#include <queue>
int main()
{
	std::queue<int> newqueue;
	int result=0;
	for (int j=1; j<=10; j++)
	newqueue.push(j);
	while (!newqueue.empty () )
	{
		result += newqueue.front ();
		newqueue.pop();
	}
	std::cout << "result is: " << result;
	return 0;
}

Output:

result is: 55

Example 2

#include <iostream>
#include <queue>
using namespace std;
int main()
{
	queue<int> newqueue;
	newqueue.push(55);
	if(newqueue.empty())
	{
		cout<<"The queue is empty";
	}
	else
	{
		cout<<"The queue is not empty";
	}
	return 0;
}

Output:

The queue is  not empty

Complexity

The complexity of the function is constant.

Data races

Only the container is accessed. By accessing the container, we come to know whether it is empty or not and based on that the value is returned.

Exception Safety

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

Next TopicC++ Queue




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