This program takes in two ordered lists and then combines them into one ordered list, then returns that new list.
I am having trouble with the insert function. I get this error:
error: no matching function for call to ‘std::list<int>::insert(std::list<int>::const_iterator&, const int&)’
copy.insert(iter1, *iter2);
I looked at the function parameters for insert and it is:
single element (1) iterator insert (iterator position, const value_type& val);
I believe i have the "const value_type& val" right because copy.push_back(*iter2) works and it has the same parameter code:
void push_back (const value_type& val);
So it is either copy that is messing it up or the "const_iterator&" parameter i have, i believe.
I know there are other ways of doing this program with just push_back and i know how to do it like that but i want to know why this solution doesn't work or at least why mine won't work.
Thank you.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
#include <list>
#include <algorithm>
#include <iostream>
#include <cassert>
using namespace std;
template <class T>
list<T> ordered_union(const list<T> & a, const list<T> & b)
{
list<T> copy = a; //makes a copy of list a
typename list<T>::const_iterator iter1 = a.begin();
typename list<T>::const_iterator iter2 = b.begin();
for(;iter1 != a.end();) {
for(; iter2 != b.end();)
if(*iter2 <= *iter1) { // if iter2 value is less than iter1 it gets inserted at iter1's position in copy
copy.insert(iter1, *iter2);
iter2++;
}
else {
iter1++;
break;
}
}
for(;iter2 != b.end();iter2++) //if iter2 is longer than iter1
copy.push_back(*iter2);
return copy;
}
///other functions and main past here
|