stl vector 函数
C ++ vector :: empty()函数 (C++ vector::empty() function)
vector::empty() is a library function of "vector" header, it is used to check whether a given vector is an empty vector or not, it returns a true if the vector size is 0, otherwise it returns false.
矢量::空()是“载体”报头的库函数,它是用来检查给定的矢量是否是一个空的载体或不是,它如果矢量大小为0,则返回真 ,否则返回假 。
Note: To use vector, include <vector> header.
注意:要使用向量,请包含<vector>标头。
Syntax of vector::empty() function
vector :: empty()函数的语法
vector::empty();
Parameter(s): void – It accepts nothing as a parameter.
参数: void –不接受任何参数。
Return value: bool – It returns true if the vector size is 0, otherwise it returns false.
返回值: 布尔 -如果矢量大小为0,则返回true,否则返回false。
Example:
例:
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
C ++程序演示vector :: empty()函数的示例 (C++ program to demonstrate example of vector::empty() function)
//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;
}
Output
输出量
Total number of elements: 0
vector is empty.
Total number of elements: 5
vector is not empty.
Reference: C++ vector::empty()
翻译自: https://www.includehelp.com/stl/vector-empty-function-with-example.aspx
stl vector 函数