empty函数
语法:
1 |
bool empty() const; |
empty() 判断Vector是否为空(返回true时为空)
源代码
1 2 3 4 5 6 |
/** * Returns true if the %vector is empty. (Thus begin() would * equal end().) */ bool empty() const { return begin() == end(); } |
测试代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
// 梁笔记 // https://zouzhongliang.com #include <iostream> #include <vector> using namespace std; int main() { vector<int> v1; vector<int> v2(10, 2); if (v1.empty()) cout<<"v1向量内无元素"<<endl; } |
测试结果:
1 |
v1向量内无元素 |