#include <list>
#include <set>
#include <iterator>
#include <algorithm>
using namespace std;
class MyContainer {
public:
string value;
MyContainer& operator=(const string& s) {
this->value = s;
return *this;
}
};
int main()
{
list<string> strings;
strings.push_back("0");
strings.push_back("1");
strings.push_back("2");
set<MyContainer> containers;
copy(strings.begin(), strings.end(), inserter(containers, containers.end()));
}
รหัสก่อนหน้าไม่ได้รวบรวม ในรูปแบบ C++ มาตรฐาน ผลลัพธ์ข้อผิดพลาดจะเป็นแบบละเอียดและเข้าใจยาก ส่วนสำคัญน่าจะเป็นเรื่องนี้...
/usr/include/c++/4.4/bits/stl_algobase.h:313: error: no match for ‘operator=’ in ‘__result.std::insert_iterator::operator* [with _Container = std::set, std::allocator >]() = __first.std::_List_iterator::operator* [with _Tp = std::basic_string, std::allocator >]()’
...ซึ่งฉันตีความหมายว่าไม่ได้กำหนดผู้ดำเนินการที่ได้รับมอบหมายที่จำเป็น ฉันได้ดูซอร์สโค้ดของ insert_iterator และสังเกตว่ามีการโอเวอร์โหลดตัวดำเนินการที่ได้รับมอบหมาย อัลกอริธึมการคัดลอกต้องใช้ตัวดำเนินการกำหนดตัววนซ้ำที่โอเวอร์โหลดเพื่อทำงาน (?)
ฉันเดาว่าเนื่องจากตัววนซ้ำอินพุตของฉันอยู่บนคอนเทนเนอร์ของสตริงและตัววนซ้ำเอาต์พุตของฉันอยู่บนคอนเทนเนอร์ของ MyContainers ซึ่งตัวดำเนินการกำหนด insert_iterator ที่โอเวอร์โหลดไม่สามารถทำงานได้อีกต่อไป
นี่เป็นการเดาที่ดีที่สุดของฉัน แต่ฉันอาจผิด
เหตุใดจึงไม่ได้ผลและฉันจะบรรลุสิ่งที่ฉันพยายามทำได้อย่างไร