vector的empty函数
时间: 2023-11-19 12:06:49 浏览: 43
vector的empty函数是用来判断当前vector是否为空的,如果为空则返回true,否则返回false。其函数原型为:
```
bool empty() const;
```
示例用法如下:
```c++
#include <iostream>
#include <vector>
int main() {
std::vector<int> v1;
std::vector<int> v2 {1, 2, 3};
if (v1.empty()) {
std::cout << "v1 is empty" << std::endl;
}
if (!v2.empty()) {
std::cout << "v2 is not empty" << std::endl;
}
return 0;
}
```
输出结果为:
```
v1 is empty
v2 is not empty
```
相关问题
c++ vector empty
在C++中,vector是一个动态数组,可以根据需要自动调整大小。要检查一个vector是否为空,可以使用empty()函数。该函数返回一个布尔值,如果vector为空,则返回true;否则返回false。以下是使用empty()函数来检查一个vector是否为空的示例代码:
```cpp
#include <iostream>
#include <vector>
int main() {
std::vector<int> myVector;
if (myVector.empty()) {
std::cout << "Vector is empty." << std::endl;
} else {
std::cout << "Vector is not empty." << std::endl;
}
return 0;
}
```
在上述代码中,我们创建了一个名为myVector的空vector,并使用empty()函数检查它是否为空。由于myVector是空的,因此输出会显示"Vector is empty."
vector empty的用法
当我们创建一个 vector 对象时,它是空的,这意味着它不包含任何元素。我们可以使用 empty() 函数来检查一个 vector 是否为空。如果 vector 为空,返回 true,否则返回 false。以下是示例代码:
```c++
#include <iostream>
#include <vector>
int main() {
std::vector<int> vec{};
if (vec.empty()) {
std::cout << "向量为空" << std::endl;
}
else {
std::cout << "向量不为空" << std::endl;
}
return 0;
}
```
输出:
```c++
向量为空
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)