C++ Stack empty() function

C++ Stack empty() function is used for testing whether the container is empty or not. In many cases, before extracting the actual elements from the stack, programmers give preference to check whether the stack does have some elements or not. Doing so is advantageous regarding memory and cost.

Syntax

Parameters

There are no parameters. Since the function is used only for testing purpose, so it is directly applied to the stack. Hence no arguments are passed.

Return value

If the container under reference is empty, then the method returns 'true' else returns 'false'. The method is used only for the purpose of testing so based on the test results values are returned.

Example 1

//The program given below is used for the detection of emptiness of a container.

Output:

Result is: 55

Example 2

//The program given below is used for the detection of emptiness of a container.

Output:

Elements are present in the stack

Complexity

The function is used only for the detection of emptiness of the container, hence accepts no parameters and has constant complexity.

Data races

Only the container is accessed. The stack is accessed to check for the presence of elements. Not all the elements are accessed by this function but a glance is made to check if the container is totally empty or has some presence.

Exception Safety

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

Next TopicC++ Stack




Latest Courses