กำหนดให้เป็นหน้าที่แสดงรายการฟังก์ชันการทำงาน =ฟังก์ชันใน C++ ใน STL
รายการใน STL คืออะไร
รายการคือคอนเทนเนอร์ที่อนุญาตให้แทรกและลบเวลาคงที่ที่ใดก็ได้ตามลำดับ Listare นำไปใช้เป็นรายการที่เชื่อมโยงเป็นสองเท่า รายการอนุญาตการจัดสรรหน่วยความจำที่ไม่ต่อเนื่องกัน List ทำการดึงข้อมูลแทรกและเคลื่อนย้ายองค์ประกอบในตำแหน่งใดๆ ในคอนเทนเนอร์ได้ดีกว่าอาร์เรย์ เวกเตอร์ และ deque ใน List การเข้าถึงองค์ประกอบโดยตรงนั้นช้าและ list นั้นคล้ายกับ toforward_list แต่ออบเจกต์รายการส่งต่อเป็นรายการที่เชื่อมโยงเดี่ยวและสามารถทำซ้ำได้เท่านั้น
ตัวดำเนินการ =ใช้อะไร
โอเปอเรเตอร์นี้ใช้เพื่อกำหนดองค์ประกอบใหม่ให้กับรายการโดยแทนที่องค์ประกอบที่มีอยู่ในรายการ และปรับเปลี่ยนขนาดของรายการใหม่ตามเนื้อหา คอนเทนเนอร์อื่นที่เรานำองค์ประกอบใหม่มาใช้มีประเภทข้อมูลเดียวกันกับคอนเทนเนอร์แรก
ไวยากรณ์:listname1 =listname2
ตัวอย่าง
Input List1: 50 60 80 90 List2: 90 80 70 60 Output List1: 90 80 70 60 Input List1: E N E R G Y List2: C A P T I O N Output List1: C A P T I O N
แนวทางสามารถติดตามได้
-
ขั้นแรก เราเริ่มต้นทั้งสองรายการ
-
จากนั้นเราใช้ =โอเปอเรเตอร์
-
จากนั้นเราก็พิมพ์รายการใหม่
โดยใช้วิธีการข้างต้น เราสามารถกำหนดองค์ประกอบใหม่ให้กับรายการได้ การทำงานของโอเปอเรเตอร์นี้คล้ายกับฟังก์ชัน swap( ) ตัวดำเนินการนี้สลับเนื้อหาของ list2 กับlist1 แต่จะไม่สลับเนื้อหาของ list1 กับ list2 และกำหนดเนื้อหาใหม่ให้กับ list1
ตัวอย่าง
// C++ code to demonstrate the working of list = operator in STL
#include<iostream.h>
#include<list.h>
Using namespace std;
int main ( ){
// initializing two lists
list<int> list1 = { 10, 20, 30, 40, 50 };
cout<< “ List1: “;
for( auto x = list1.begin( ); x != list1.end( ); ++x)
cout<< *x << “ “;
list<int> list2 = { 40, 50, 60, 70, 80 };
cout<< “ List2: “;
for( auto x = list2.begin( ); x != list2.end( ); ++x)
cout<< *x << “ “;
list1 = list2;
// printing new content of list
cout<< “ New contents of List1 is :”;
for(auto x = list1.begin( ); x != list1.end( ); ++x)
cout<< *x<< “ “;
return 0;
} ผลลัพธ์
หากเรารันโค้ดด้านบน มันจะสร้างผลลัพธ์ต่อไปนี้
Input - List1: 10 20 30 40 50 List2: 40 50 60 70 80 Output - New content of List1 is: 40 50 60 70 80
ตัวอย่าง
// C++ code to demonstrate the working of list = operator in STL
#include<iostream.h>
#include<list.h>
Using namespace std;
int main ( ){
// initializing two lists
list<char> list1 = { 'C', 'H', 'A', 'R', 'G', 'E', 'R' };
cout<< " List1: ";
for( auto x = list1.begin( ); x != list1.end( ); ++x)
cout<< *x << " ";
List<char> list2 = { 'P', 'O', 'I', 'N', 'T' };
cout<< " List2: ";
for( auto x = list2.begin( ); x != list2.end( ); ++x)
cout<< *x << " ";
list1 = list2;
// printing new content of list
cout<< " New contents of List1 is :";
for(auto x = list1.begin( ); x != list1.end( ); ++x)
cout<< *x<< " ";
return 0;
} ผลลัพธ์
หากเรารันโค้ดด้านบน มันจะสร้างผลลัพธ์ต่อไปนี้
Input - List1: C H A R G E R List2: P O I N T Output - New contents of List1 is: P O I N T