I just wrote code to test the behavior of std :: equal and left in surprise:
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(); } }
Result (surprise for me):
Error: comparing 2 lists where one is not empty should not be equal
Observation: why does this std :: equal not check first whether 2 containers have the same size() ? Was there a legitimate reason?
c ++ stl
sivabudh
source share