When using copy, remove_copy, et al algorithms that potentially copy from one sequence to another, make sure the target container is at least as large as the resultant container is likely to get. You have to be extra careful when copying from one list (populated) to another (just defined, so unpopulated).
Consider the following:
list<int> list1; // // populate list1 with what-not. // list<int> list2; // DOESN'T WORK. copy(list1.begin(), list1.end(), list2.begin()); // this works because back_list_iterator invokes push_back() which // dynamically resizes the list as appropriate. Note that if the // target were a set, inserter would've been // the appropriate one.
back_insert_iterator<list<int> > back_list_iterator( list2 );
copy(list1.begin(), list1.end(), back_list_iterator);
or
list2.insert(list2.end(), list1.begin(), list1.end());