I just wrote some code to test the behavior of std::equal, and came away surprised:
int main()
{
try
{
std::list<int> lst1;
std::list<int> lst2;
if(!std::equal(lst1.begin(), lst1.end(), lst2.begin()))
throw std::logic_error("Error: 2 empty lists should always be equal");
lst2.push_back(5);
if(std::equal(lst1.begin(), lst1.end(), lst2.begin()))
throw std::logic_error("Error: comparing 2 lists where one is not empty should not be equal");
}
catch(std::exception& e)
{
std::cerr << e.what();
}
}
The output (a surprise to me):
Error: comparing 2 lists where one is not empty should not be equal
Observation: why is it the std::equal does not first check if the 2 containers have the same size()
? Was there a legitimate reason?