C ++ STL vector :: función vacía () : Aquí, vamos a aprender acerca de la función de vaciar () del vector de la cabecera en C ++ con STL ejemplo .
función
C ++ vector :: vacío ()
vector :: vacía () es una función de biblioteca de “vector” cabecera, se utiliza para comprobar si un vector dado es un vector vacío o no , devuelve un cierto si el tamaño del vector es 0, de lo contrario retorna falso .
Nota: Para uso vector, class & lt; vector & gt; cabecera.
Sintaxis del vector :: vacío () la función
vector::empty();
Parámetro (s): include – Se acepta nada como un parámetro.
class valor: bool – Devuelve cierto si el tamaño del vector es 0, de lo contrario devuelve falso .
Ejemplo:
Input:
vector<int> vector1{ 1, 2, 3, 4, 5 };
vector<int> vector2;
Function call:
cout << vector1.empty() << endl;
cout << vector2.empty() << endl;
Output:
false
true
programa en C ++ para demostrar ejemplo de vector :: vacío () función
//C++ STL program to demonstrate example of
//vector::empty() function
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> v1;
//printing the size of the vector
cout << "Total number of elements: " << v1.size() << endl;
//checking whether vector is empty or not
if (v1.empty())
cout << "vector is empty." << endl;
else
cout << "vector is not empty." << endl;
//pushing elements
v1.push_back(10);
v1.push_back(20);
v1.push_back(30);
v1.push_back(40);
v1.push_back(50);
//printing the size of the vector
cout << "Total number of elements: " << v1.size() << endl;
//checking whether vector is empty or not
if (v1.empty())
cout << "vector is empty." << endl;
else
cout << "vector is not empty." << endl;
return 0;
}
salida
Total number of elements: 0
vector is empty.
Total number of elements: 5
vector is not empty.
Referencia: C ++ vector :: vacío ()