×

C++ STL Tutorial

C++ STL Algorithm

C++ STL Arrays

C++ STL String

C++ STL List

C++ STL Stack

C++ STL Set

C++ STL Queue

C++ STL Vector

C++ STL Map

C++ STL Multimap

C++ STL MISC.

array::empty() in C++ STL with Example

C++ STL array::empty() function with Example: Here, we are going to learn about a library function empty() of array class, which is used to check whether an array is empty or not.
Submitted by IncludeHelp, on November 14, 2018

"array" is a container which is used to create/container which is used to create/contains the fixed size arrays, the "array" in C++ STL is "class" actually and they are more efficient, lightweight and very easy to use, understand, "array" class contains many inbuilt functions, thus the implementation of the operations are fast by using this rather that C-Style arrays.

To use "array" class and its function, we need to include "array" class and its function, we need to include "array" header.

C++ STL array::empty() Function

empty() function is used to check whether an array is empty or not. It returns 1 (true), if the array size is 0 and returns 0 (false), if array size is not zero.

Syntax

array_name.empty();

Parameter(s)

There is no parameter to be passed.

Return value

  • 1 (true) – if array is empty
  • 0 (false) – if array is not empty

Sample Input and Output

Input:
arr1{} //an empty array
arr2{10,20,30} //array with 3 elements

Function calls:
arr1.empty()
arr2.empty()

Output:
1
0

Example

#include <array>
#include <iostream>
using namespace std;

int main() {
  // declaring two arrays
  array<int, 0> arr1{};
  array<int, 5> arr2{};
  array<int, 5> arr3{10, 20, 30};
  array<int, 5> arr4{10, 20, 30, 40, 50};

  // printing arr_name.empty() value
  cout << "arr1.empty(): " << arr1.empty() << endl;
  cout << "arr2.empty(): " << arr2.empty() << endl;
  cout << "arr3.empty(): " << arr3.empty() << endl;
  cout << "arr4.empty(): " << arr4.empty() << endl;

  // checking and printing messages
  if (arr1.empty())
    cout << "arr1 is empty" << endl;
  else
    cout << "arr1 is not empty" << endl;

  if (arr2.empty())
    cout << "arr2 is empty" << endl;
  else
    cout << "arr2 is not empty" << endl;

  if (arr3.empty())
    cout << "arr3 is empty" << endl;
  else
    cout << "arr3 is not empty" << endl;

  if (arr4.empty())
    cout << "arr4 is empty" << endl;
  else
    cout << "arr4 is not empty" << endl;

  return 0;
}

Output

arr1.empty(): 1
arr2.empty(): 0
arr3.empty(): 0
arr4.empty(): 0
arr1 is empty
arr2 is not empty
arr3 is not empty
arr4 is not empty 

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.