C++ vector::empty() 函數
vector::empty() 是 "vector" 頭文件的庫函數,用於檢查給定的向量是否為空向量,如果向量大小為 0 則返回真,否則返回假。
注意:要使用矢量,請包括<vector>
標題。
vector::empty() 函數的語法
vector::empty();
參數: void
– 它不接受任何參數。
返回值: bool
– 如果向量大小為 0,則返回 true,否則返回 false。
例:
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++ 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;
}
輸出
Total number of elements:0 vector is empty. Total number of elements:5 vector is not empty.
參考:C++ vector::empty()
相關用法
- C++ vector::empty()、vector::size()用法及代碼示例
- C++ vector::emplace_back用法及代碼示例
- C++ vector::erase()用法及代碼示例
- C++ vector::end()用法及代碼示例
- C++ vector::max_size()用法及代碼示例
- C++ vector::rbegin()用法及代碼示例
- C++ vector::pop_back()用法及代碼示例
- C++ vector::crend()用法及代碼示例
- C++ vector::push_back()用法及代碼示例
- C++ vector::at()用法及代碼示例
- C++ vector::swap()用法及代碼示例
- C++ vector::shrink_to_fit()用法及代碼示例
- C++ vector::cbegin()用法及代碼示例
- C++ vector::back()用法及代碼示例
- C++ vector::assign()用法及代碼示例
- C++ vector::begin()用法及代碼示例
- C++ vector::cend()用法及代碼示例
- C++ vector::operator[]用法及代碼示例
- C++ vector::clear()用法及代碼示例
- C++ vector::cbegin()、vector::cend()用法及代碼示例
注:本文由純淨天空篩選整理自 vector::empty() function with example in C++ STL。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。