Can copy() be used for initializing a container? The following code has a runtime error: “list iterator not dereferencable”. What would be the reason for this? Thanks
string a[] = {"00", "11", "22"};
list<string> list_1(a, a+3), list_2;
copy(list_1.begin(), list_1.end(), list_2.begin()); // error:
list_2has no space where to copy the source range. You have to either reserve enough space, or use an inserter iterator. Alternatively, you could just use thelistconstructor:or further on the
assignmember function: