×

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.

Check vector is empty or not | C++ STL

Here, we are going to learn how to check whether a vector is empty or not in C++ STL (Standard Template Library)? There are two ways to check whether list is empty or not 1) using vector::empty() and 2) vector::size().
Submitted by IncludeHelp, on August 24, 2018

Vector are dynamic arrays in nature, they can resize themselves according to the number of elements, if elements are added or deleted, size or memory of the vector is automatically resized.

There are two methods to check whether a given vector is an empty vector or not.

  1. Using vector::empty() function
  2. Using vector::size() function

Check vector is empty using vector::empty() function

It returns true, if vector is empty else it returns false.

Syntax

vector_name.empty();

Sample Input and Output:

Input: 
vector<int> v1{10, 20, 30, 40, 50 };
vector<int> v2{};
    
Function calls:
v1.empty()
v2.empty()
    
Output:
false
true

C++ program to check vector is empty vector::empty() function

#include <iostream>
#include <vector>

using namespace std;

int main() {
  // declare and assign a vectors
  // non-empty
  vector<int> v1{10, 20, 30, 40, 50};
  // empty
  vector<int> v2{};

  // check whether vector are empty or not
  // using empty () function
  if (v1.empty())
    cout << "vector: v1 is an empty vector" << endl;
  else
    cout << "vector: v1 has " << v1.size() << " elements" << endl;

  if (v2.empty())
    cout << "vector: v2 is an empty vector" << endl;
  else
    cout << "vector: v2 has " << v2.size() << " elements" << endl;

  return 0;
}

Output

vector: v1 has 5 elements
vector: v2 is an empty vector

Check vector is empty using vector::size() function

It returns total number of elements of a vector.

Syntax

 vector_name.size();

Sample Input and Output:

Input: 
vector<int> v1{10, 20, 30, 40, 50 };
vector<int> v2{};
    
Function calls:
v1.size()
v2.size()
    
Output:
5
0

C++ program to check vector is empty vector::size() function

#include <iostream>
#include <vector>

using namespace std;

int main() {
  // declare and assign a vectors
  // non-empty
  vector<int> v1{10, 20, 30, 40, 50};
  // empty
  vector<int> v2{};

  // variable to store size
  int n = 0;

  // check whether vector are empty or not
  // using size () function
  n = v1.size();
  if (n == 0)
    cout << "Vector: v1 is an empty vector" << endl;
  else
    cout << "vector: v1 has " << n << " elements" << endl;

  n = v2.size();
  if (n == 0)
    cout << "vector: v2 is an empty vector" << endl;
  else
    cout << "vector: v2 has " << v2.size() << " elements" << endl;

  return 0;
}

Output

vector: v1 has 5 elements
vector: v2 is an empty vector

Related Tutorials

Comments and Discussions!

Load comments ↻





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