Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why wasn't std::forward_list given a count() member function?

I understand why std::forward_list does not have a size() member function, since an O(1) version would mess up the complexity of certain splice() overloads, and since an O(N) version would be inconsistent with all the rest of the Standard Library's containers.

It is also true that both std::list and std::forward_list already have several other member functions with the same semantics as their cousins from the <algorithm> corner of the Standard Library (merge(), reverse(), remove(), remove_if(), unique(), sort()).

So why wasn't a count() member function of O(N) complexity provided to std::forward_list that had the semantics of returning std::distance(std::begin(some_list), std::end(some_list))?

like image 830
TemplateRex Avatar asked Apr 29 '13 13:04

TemplateRex


1 Answers

The member functions you mention (merge(), reverse(), remove(), remove_if(), unique(), sort()) are provided because they have better complexity than the generic algorithms in the <algorithm> standard headers.

A member function such as count(), on the other hand, would not have better complexity than std::distance(std::begin(some_list), std::end(some_list)).

Also, it may be misinterpreted as the better-complexity version of the std::count generic algorithm, which does something basically different.

like image 135
Andy Prowl Avatar answered Oct 03 '22 05:10

Andy Prowl