Chuyển tới nội dung chính

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

  1. 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.
  2. 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.
  3. deque& operator=(initializer_list<value_type> ilist): Gán các phần tử từ danh sách khởi tạo ilist cho deque hiện tại. deque hiện tại sẽ bị xóa nội dung cũ trước khi gán.
  4. 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.
  5. 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.
  6. 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.
  7. 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án a = a;
  8. 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.
  9. Độ 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)).

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

assignGán các phần tử mới cho deque, thay thế nội dung hiện tại của nó