std::deque::operator=
#include <deque>
// 1. Copy assignment operator
deque& operator=(const deque& other);
// 2. Move assignment operator (C++11)
deque& operator=(deque&& other) noexcept;
// 3. Initializer list assignment operator (C++11)
deque& operator=(initializer_list<value_type> ilist);
Gán nội dung của một deque cho một deque khác.
Tham số
other
- Deque nguồn cần gán.
ilist
- Danh sách khởi tạo chứa các phần tử cần gán.
Giá trị trả về
- Tham chiếu đến deque hiện tại (sau khi đã gán).
Đặc điểm
deque& operator=(const deque& other)
: Sao chép tất cả các phần tử từother
sang deque hiện tại. deque hiện tại sẽ bị xóa nội dung cũ trước khi sao chép.deque& operator=(deque&& other)
: Di chuyển tất cả các phần tử từother
sang deque hiện tại.other
sẽ trở thành rỗng sau khi di chuyển.deque& operator=(initializer_list<value_type> ilist)
: Gán các phần tử từ danh sách khởi tạoilist
cho deque hiện tại. deque hiện tại sẽ bị xóa nội dung cũ trước khi gán.- Thay thế nội dung: Các toán tử gán thay thế toàn bộ nội dung hiện tại của deque bằng nội dung mới.
- Sao chép và di chuyển:
- Sao chép: tạo một bản sao độc lập của deque nguồn.
- Di chuyển: di chuyển tài nguyên (dữ liệu) từ deque nguồn sang deque đích, làm cho deque nguồn trở thành rỗng, hiệu quả hơn sao chép.
- Initializer list: Phiên bản gán từ danh sách khởi tạo cung cấp cách thức tiện lợi để gán các giá trị cố định cho deque.
- Toán tử
operator=
được cài đặt để tự phát hiện và xử lí đúng các trường hợp tự gána = a;
- Phiên bản gán di chuyển được đánh dấu là
noexcept
, nghĩa là nó được đảm bảo không ném ra ngoại lệ nào. - Độ phức tạp:
- Sao chép: Độ phức tạp tuyến tính theo kích thước của deque nguồn (
O(n)
). - Di chuyển: Độ phức tạp thường là hằng số (
O(1)
). - Danh sách khởi tạo: Độ phức tạp tuyến tính theo kích thước của danh sách khởi tạo (
O(n)
).
- Sao chép: Độ phức tạp tuyến tính theo kích thước của deque nguồn (
Ví dụ
#include <iostream>
#include <deque>
int main() {
std::deque<int> deque1 = {1, 2, 3};
std::deque<int> deque2 = {4, 5};
std::deque<int> deque3;
// 1. Copy assignment
deque3 = deque1;
std::cout << "deque3 after copy assignment from deque1:";
for (int x : deque3) std::cout << ' ' << x; // Output: deque3 after copy assignment from deque1: 1 2 3
std::cout << '\n';
// 2. Move assignment
deque3 = std::move(deque2);
std::cout << "deque3 after move assignment from deque2:";
for (int x : deque3) std::cout << ' ' << x; // Output: deque3 after move assignment from deque2: 4 5
std::cout << '\n';
std::cout << "deque2 after move assignment:";
for (int x : deque2) std::cout << ' ' << x; // Output: deque2 after move assignment: (empty)
std::cout << '\n';
// 3. Initializer list assignment
deque3 = {7, 8, 9, 10};
std::cout << "deque3 after initializer list assignment:";
for (int x : deque3) std::cout << ' ' << x; // Output: deque3 after initializer list assignment: 7 8 9 10
std::cout << '\n';
return 0;
}
Các hàm liên quan
assign | Gán các phần tử mới cho deque, thay thế nội dung hiện tại của nó |