std::multiset::operator=
#include <set>
// 1. Copy assignment operator
multiset& operator=(const multiset& other);
// 2. Move assignment operator (C++11)
multiset& operator=(multiset&& other) noexcept;
// 3. Initializer list assignment operator (C++11)
multiset& operator=(initializer_list<value_type> ilist);
Gán nội dung của một std::multiset khác hoặc một initializer_list cho std::multiset hiện tại.
Tham số
other
- std::multiset 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ề
multiset&
- Tham chiếu đến std::multiset 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::multiset 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::multiset nguồn.
- Move assignment di chuyển tài nguyên (dữ liệu) từ std::multiset nguồn sang std::multiset đích, làm cho std::multiset 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::multiset.
- 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ử, multiset đích sẽ không thay đổi. Nếu exception xảy ra khi move assignment đang move các phần tử, thì multiset 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::multiset 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::multiset nguồn (
Ví dụ
#include <iostream>
#include <set>
int main() {
std::multiset<int> mset1 = {1, 2, 3, 3};
std::multiset<int> mset2 = {4, 5, 4};
std::multiset<int> mset3;
// 1. Copy assignment
mset3 = mset1;
std::cout << "mset3 after copy assignment from mset1:";
for (int x : mset3) std::cout << ' ' << x; // Output: mset3 after copy assignment from mset1: 1 2 3 3
std::cout << '\n';
// 2. Move assignment
mset3 = std::move(mset2);
std::cout << "mset3 after move assignment from mset2:";
for (int x : mset3) std::cout << ' ' << x; // Output: mset3 after move assignment from mset2: 4 4 5
std::cout << '\n';
std::cout << "mset2 after move assignment:";
for (int x : mset2) std::cout << ' ' << x; // Output: mset2 after move assignment: (empty)
std::cout << '\n';
// 3. Initializer list assignment
mset3 = {7, 8, 9, 10, 8};
std::cout << "mset3 after initializer list assignment:";
for (int x : mset3) std::cout << ' ' << x; // Output: mset3 after initializer list assignment: 7 8 8 9 10
std::cout << '\n';
return 0;
}
Các hàm liên quan
insert | Chèn một hoặc nhiều phần tử mới vào std::multiset |