【发布时间】:2010-04-24 18:41:47
【问题描述】:
认为这很简单。
但是在运行下面的代码时,我得到一个“iterator not dereferencable”错误。
怎么了?
template<typename T>
struct SumsTo : public std::binary_function<T, T, bool>
{
int myInt;
SumsTo(int a)
{
myInt = a;
}
bool operator()(const T& l, const T& r)
{
cout << l << " + " << r;
if ((l + r) == myInt)
{
cout << " does add to " << myInt;
}
else
{
cout << " DOES NOT add to " << myInt;
}
return true;
}
};
void main()
{
list<int> l1;
l1.push_back(1);
l1.push_back(2);
l1.push_back(3);
l1.push_back(4);
list<int> l2;
l2.push_back(9);
l2.push_back(8);
l2.push_back(7);
l2.push_back(6);
transform(l1.begin(), l1.end(), l2.begin(), l2.end(), SumsTo<int>(10) );
}
【问题讨论】: