std::unordered_set::operator=
#include <unordered_set>
// 1. Copy assignment operator
unordered_set& operator=(const unordered_set& other);
// 2. Move assignment operator (C++11)
unordered_set& operator=(unordered_set&& other);
// 3. Initializer list assignment operator (C++11)
unordered_set& operator=(initializer_list<value_type> ilist);
Gán nội dung của một std::unordered_set khác hoặc một initializer_list cho std::unordered_set hiện tại.
Tham số
other
- std::unordered_set nguồn cần gán (Phiên bản 1 và 2).
ilist
- Danh sách khởi tạo chứa các phần tử cần gán (Phiên bản 3).
Giá trị trả về
unordered_set&
- Tham chiếu đến std::unordered_set hiện tại (sau khi đã gán).
Đặc điểm
- 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_set bằng nội dung mới.
- Copy vs. Move:
- Copy assignment tạo một bản sao độc lập của std::unordered_set nguồn.
- Move assignment di chuyển tài nguyên (dữ liệu) từ std::unordered_set nguồn sang std::unordered_set đích, làm cho std::unordered_set 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 std::unordered_set.
- 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ána = a;
- An toàn exception: Nếu exception xảy ra trong khi copy assignment đang copy các phần tử, unordered_set đích sẽ không thay đổi. Nếu exception xảy ra khi move assignment đang move các phần tử, thì unordered_set nguồn vẫn hợp lệ nhưng trạng thái không xác định.
- 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. - Độ phức tạp:
- Copy assignment: Độ phức tạp tuyến tính theo kích thước của std::unordered_set 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)
).
- Copy assignment: Độ phức tạp tuyến tính theo kích thước của std::unordered_set nguồn (
Ví dụ
#include <iostream>
#include <unordered_set>
int main() {
std::unordered_set<int> set1 = {1, 2, 3};
std::unordered_set<int> set2 = {4, 5};
std::unordered_set<int> set3;
// 1. Copy assignment
set3 = set1;
std::cout << "set3 after copy assignment from set1:";
for (int x : set3) std::cout << ' ' << x; // Output: set3 after copy assignment from set1: 1 2 3 (thứ tự có thể khác)
std::cout << '\n';
// 2. Move assignment
set3 = std::move(set2);
std::cout << "set3 after move assignment from set2:";
for (int x : set3) std::cout << ' ' << x; // Output: set3 after move assignment from set2: 4 5 (thứ tự có thể khác)
std::cout << '\n';
std::cout << "set2 after move assignment:";
for (int x : set2) std::cout << ' ' << x; // Output: set2 after move assignment: (empty)
std::cout << '\n';
// 3. Initializer list assignment
set3 = {7, 8, 9, 10, 9}; // 9 bị loại bỏ do trùng lặp
std::cout << "set3 after initializer list assignment:";
for (int x : set3) std::cout << ' ' << x; // Output: set3 after initializer list assignment: 7 8 9 10 (thứ tự có thể khác)
std::cout << '\n';
return 0;
}
Các hàm liên quan
emplace | Xây dựng (construct) một phần tử mới trực tiếp trong std::unordered_set |