Bài viết trình bày chức năng của hàm deque::max_size() trong C++ STL.
Deque là gì?
Deque (Double Ended Queue) là container trình tự cung cấp chức năng mở rộng và co hẹp ở cả hai đầu. Queue thông thường chỉ cho phép chèn ở cuối và xóa ở đầu, còn Deque cho phép chèn và xóa ở cả hai đầu.
deque::max_size() là gì?
Hàm này dùng để kiểm tra số lượng phần tử tối đa mà deque có thể chứa.
Cú pháp: dequename.max_size()
Trả về – Số lượng phần tử tối đa deque có thể chứa.
Ví dụ
Input Deque: 12 13 14 15 16 Output Maximum size: 4611686018427387903 Input Deque: F O R C E Output Maximum size: 4611686018427387903
Cách tiếp cận
Khai báo deque.
In deque.
Kiểm tra kích thước tối đa của deque.
Theo cách này có thể kiểm tra kích thước tối đa của deque.
Ví dụ hoàn chỉnh 1
// C++ code to demonstrate the working of deque max_size() function
#include <iostream>
#include <deque>
using namespace std;
int main() {
// khởi tạo deque
deque<int> deque = { 11, 12, 13, 14, 15 };
// in deque
cout << " Deque: ";
for (auto x = deque.begin(); x != deque.end(); ++x)
cout << *x << " ";
cout << " The maximum size of deque: " << deque.max_size();
return 0;
}
Kết quả
Input - Deque: 11 12 13 14 15 Output - The maximum size of deque: 461168601842738790
Ví dụ hoàn chỉnh 2
// C++ code to demonstrate the working of deque max_size() function
#include <iostream>
#include <deque>
using namespace std;
int main() {
// khởi tạo deque
deque<int> deque = { 14, 15, 16, 17, 18, 19, 20 };
// in deque
cout << " Deque: ";
for (auto x = deque.begin(); x != deque.end(); ++x)
cout << *x << " ";
cout << "Maximum size of deque: " << deque.max_size();
return 0;
}
Kết quả
Input: Deque:14 15 16 17 18 19 20 Output: Maximum size of deque: 4611686018427387903