ตัวดำเนินการฟังก์ชัน=ถูกใช้ในชุดเพื่อคัดลอกชุดหนึ่ง (หรือย้ายไปที่ชุดอื่นใน C++ STL ซึ่งทำงานเหมือนการดำเนินการกำหนด '=' ปกติสำหรับชุด มีรูปแบบที่มากเกินไปของฟังก์ชันนี้ -
-
คัดลอก :- set&operator=(const set&s1) −
ฟังก์ชันนี้จะคัดลอกองค์ประกอบทั้งหมดในชุด s1 ไปยังชุดอื่น พารามิเตอร์ที่ส่งผ่านเป็นชุดประเภทเดียวกัน
-
การใช้งาน - ตั้งค่า s1=s2;
-
ย้าย :- set&operator=( set &&s1 ) −
สิ่งนี้จะย้ายองค์ประกอบของชุด s1 ไปยังชุดการโทร
-
รายการตัวเริ่มต้น :- set&operator=(initializer_list
ilist) −เวอร์ชันนี้คัดลอกค่าของ initializer list ilist ไปยังชุดการโทร
การใช้งาน − set
s1={ 1,2,3,4,5 };
หมายเหตุ - ทั้งหมดส่งคืนการอ้างอิงของตัวชี้ของประเภท set
โปรแกรมต่อไปนี้ใช้เพื่อสาธิตการใช้ฟังก์ชันปัดเศษในโปรแกรม C++ -
ตัวอย่าง
#include <iostream>
#include <set>
using namespace std;
// merge function
int main(){
set<int> set1, set2;
// List initialization
set1 = { 1, 2, 3, 4, 5 };
set2 = { 10,11,12,13 };
// before copy
cout<<"set1 :";
for (auto s = set1.begin(); s != set1.end(); ++s) {
cout << *s << " ";
}
cout << endl;
cout<<"set2 :";
for (auto s = set2.begin(); s != set2.end(); ++s) {
cout << *s << " ";
}
//after copy set1 to set2
cout<<endl<<"After Copy"<<endl;
cout<<"set1 :";
set1=set2;
for (auto s = set1.begin(); s != set1.end(); ++s) {
cout << *s << " ";
}
return 0;
} ผลลัพธ์
set1 :1 2 3 4 5 set2 :10 11 12 13 After Copy set1 :10 11 12 13