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

std::unordered_multimap::operator=

#include <unordered_map>

// 1. Copy assignment operator
unordered_multimap& operator=(const unordered_multimap& other);

// 2. Move assignment operator (C++11)
unordered_multimap& operator=(unordered_multimap&& other);

// 3. Initializer list assignment operator (C++11)
unordered_multimap& operator=(initializer_list<value_type> ilist);

Gán nội dung của một std::unordered_multimap khác hoặc một initializer_list cho std::unordered_multimap hiện tại.

Tham số

other

  • (Phiên bản 1 và 2) std::unordered_multimap nguồn cần gán.

ilist

  • (Phiên bản 3) Danh sách khởi tạo chứa các phần tử cần gán.

Giá trị trả về

unordered_multimap&

  • Tham chiếu đến std::unordered_multimap hiện tại (sau khi đã gán).

Đặc điểm

  1. 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 std::unordered_multimap bằng nội dung mới.
  2. Copy vs. Move:
    • Copy assignment tạo một bản sao độc lập của std::unordered_multimap nguồn.
    • Move assignment di chuyển tài nguyên (dữ liệu) từ std::unordered_multimap nguồn sang std::unordered_multimap đích, làm cho std::unordered_multimap nguồn trở thành rỗng, hiệu quả hơn sao chép.
  3. 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 std::unordered_multimap.
  4. Tự gán (self-assignment): 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;
  5. An toàn exception: Nếu exception xảy ra trong khi copy assignment đang copy các phần tử, unordered_multimap đích sẽ không thay đổi. Nếu exception xảy ra khi move assignment đang move các phần tử, thì unordered_multimap nguồn vẫn hợp lệ nhưng trạng thái không xác định.
  6. noexcept: Move assignment operator được đánh dấu là noexcept, nghĩa là nó được đảm bảo không ném ra ngoại lệ nào.
  7. Làm thay đổi iterator: Toán tử gán làm thay đổi (invalidate) các iterator đang trỏ đến std::unordered_multimap.
  8. Độ phức tạp:
    • Copy assignment: Độ phức tạp tuyến tính theo kích thước của std::unordered_multimap nguồn (O(n)).
    • Move assignment: Độ phức tạp thường là hằng số (O(1)).
    • Initializer list assignment: Độ 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 <unordered_map>
#include <string>

int main() {
std::unordered_multimap<std::string, int> ummap1 = {{"apple", 1}, {"banana", 2}, {"apple", 3}};
std::unordered_multimap<std::string, int> ummap2 = {{"grape", 4}, {"kiwi", 5}, {"grape", 6}};
std::unordered_multimap<std::string, int> ummap3;

// 1. Copy assignment
ummap3 = ummap1;
std::cout << "ummap3 after copy assignment from ummap1:\n";
for (const auto& pair : ummap3) {
std::cout << pair.first << ": " << pair.second << '\n';
}
// Output:
// ummap3 after copy assignment from ummap1:
// apple: 1
// apple: 3
// banana: 2
// (thứ tự có thể khác)

// 2. Move assignment
ummap3 = std::move(ummap2);
std::cout << "ummap3 after move assignment from ummap2:\n";
for (const auto& pair : ummap3) {
std::cout << pair.first << ": " << pair.second << '\n';
}
// Output:
// ummap3 after move assignment from ummap2:
// grape: 4
// kiwi: 5
// grape: 6
// (thứ tự có thể khác)

std::cout << "ummap2 after move assignment:\n";
for (const auto& pair : ummap2) {
std::cout << pair.first << ": " << pair.second << '\n';
}
// Output:
// ummap2 after move assignment:
// (empty)

// 3. Initializer list assignment
ummap3 = {{"pear", 6}, {"mango", 7}, {"pear", 8}, {"mango", 7}};
std::cout << "ummap3 after initializer list assignment:\n";
for (const auto& pair : ummap3) {
std::cout << pair.first << ": " << pair.second << '\n';
}
// Output:
// ummap3 after initializer list assignment:
// pear: 6
// pear: 8
// mango: 7
// mango: 7
// (thứ tự có thể khác)

return 0;
}

Các hàm liên quan

emplaceXây dựng (construct) một phần tử mới (cặp key-value) trực tiếp trong std::unordered_multimap tại vị trí thích hợp