我有一個std::vector<bool>
實例(foo
,說),我需要寫一個返回true
如果所有元素都true
功能。爲什麼的std :: min_element和公司沒有專門針對性病::矢量<bool>
我用
return *std::min_element(foo.begin(), foo.end());
實現這一目標,但是這已經讓我思考:你知道的最小元素false
,如果容器包含至少一個false
值。換句話說,你不需要遍歷整個容器到end()
,這意味着專業化std::min_element
是適當的,就像std::vector<bool>
專業化被認爲是適當的。
我錯過了什麼?或者這會被視爲過早優化?無論如何,一個好的編譯器可能會把它整理出來。
這似乎更適合['std :: all_of'](http://en.cppreference.com/w/cpp/algorithm/all_any_none_of)。 –
爲什麼不使用bitset? –
爲什麼不使用內置短路的'std :: any_of'? – NathanOliver