1. deque 的大小
- deque.size(); //返回容器中元素的个数
1 #include <iostream> 2 #include <deque> 3 4 using namespace std; 5 6 int main() 7 { 8 deque<int> deqInt_A; 9 deqInt_A.assign(4, 111); 10 11 cout << "deqInt_A 中的元素个数为:"<< deqInt_A.size() << endl; 12 13 return 0; 14 }
打印结果:
- deque.empty(); //判断容器是否为空
1 #include <iostream> 2 #include <deque> 3 4 using namespace std; 5 6 int main() 7 { 8 deque<int> deqInt_A,deqInt_B; 9 deqInt_A.assign(4, 111); 10 11 if (deqInt_A.empty()) 12 { 13 cout << "deqInt_A 为空" << endl; 14 } 15 else 16 { 17 cout << "deqInt_A 不为空" << endl; 18 } 19 if (deqInt_B.empty()) 20 { 21 cout << "deqInt_B 为空" << endl; 22 } 23 else 24 { 25 cout << "deqInt_B 不为空" << endl; 26 } 27 28 return 0; 29 }
打印结果
- deque.resize(num); //重新指定容器的长度为num,若容器变长,则以默认值0填充新位置。如果容器变短,则末尾超出容器长度的元素被删除。
1 #include <iostream> 2 #include <deque> 3 4 using namespace std; 5 6 int main() 7 { 8 deque<int> deqInt_A; 9 deqInt_A.assign(5, 111); 10 11 for (deque<int>::iterator it = deqInt_A.begin(); it!= deqInt_A.end(); it++) 12 { 13 cout << *it << " "; 14 } 15 16 cout << "\n使用 resize 扩容" << endl; 17 deqInt_A.resize(10); 18 for (deque<int>::iterator it = deqInt_A.begin(); it != deqInt_A.end(); it++) 19 { 20 cout << *it << " "; 21 } 22 23 return 0; 24 }
打印结果:
- deque.resize(num, elem); //重新指定容器的长度为num,若容器变长,则以elem值填充新位置。如果容器变短,则末尾超出容器长度的元素被删除。
1 #include <iostream> 2 #include <deque> 3 4 using namespace std; 5 6 int main() 7 { 8 deque<int> deqInt_A; 9 deqInt_A.assign(5, 111); 10 11 for (deque<int>::iterator it = deqInt_A.begin(); it!= deqInt_A.end(); it++) 12 { 13 cout << *it << " "; 14 } 15 16 cout << "\n使用 resize 扩容" << endl; 17 deqInt_A.resize(10, 222); 18 for (deque<int>::iterator it = deqInt_A.begin(); it != deqInt_A.end(); it++) 19 { 20 cout << *it << " "; 21 } 22 23 return 0; 24 }
打印结果:
====================================================================================================================