#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
usingnamespace std;
int main() {
vector<int> ivec;
list<int> ilst;
for(vector<int>::size_type s = 1; s <= 10; ++s)
ivec.push_back(s);
//The following line of code Will Not insert anything to ilst
//unique_copy(ivec.begin(), ivec.end(), ilst.begin());
//Only the adaptor can do the inserting
unique_copy(ivec.begin(), ivec.end(), back_inserter(ilst));
for(list<int>::iterator i = ilst.begin(); i != ilst.end(); ++i)
cout << *i << "\t";
return 0;
}
Why can't I use ilst.begin() as an argument for unique_copy to insert elements ?
Why can only the adaptor (in this case back_inserter) get the job done?
If we expand on firedraco's answer,there should be enough room in the destination container between the given start iterator and container.end() to fit in all the possible values or
you will get that error because the destination container won't automatically expand to fit.
Yes, basically because the container needs to be expanded so the new elements fit.
back_inserter() does nothing more than call push_back() on the element to insert it.
Thank you very much, firedraco's answer and the extended explanation by guestgulkan seem very correct and acceptable to me. it does make sense that in between iterators wouldn't be much room for insertion.
Thank you very much once more for all the support.