Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

STL algorithms taking the whole container rather than .begin(), end() as arg? [duplicate]

Stand-alone STL algorithms (like std::count_if) take pair of iterators. In all cases where I use those (and in all examples I've seen online!), I find myself typing

std::count_if(myContainer.begin(),myContainer.end(), /* ... */ );

Is there a reason why shorthand templates of the style

std::count_if(myContainer, /* ... */ );

are not provided, given that more of than not is the operaation performed on the whole container? Did I just overlook it? Is the answer different for c++11 and c++03?

like image 231
eudoxos Avatar asked Nov 17 '11 09:11

eudoxos


People also ask

How are the STL algorithm implemented?

STL supports various algorithms that act on containers through iterators. As these algorithms act on iterators and not directly on containers, they can be used on any type of iterators. STL algorithms are built-in and thus save a lot of time and are more reliable too. They also enhance code reusability.

What is an STL algorithm?

The Standard Template Library, or STL, is a C++ library of container classes, algorithms, and iterators; it provides many of the basic algorithms and data structures of computer science. The STL is a generic library, meaning that its components are heavily parameterized: almost every component in the STL is a template.

How many STL algorithms are there?

Iterators. The STL implements five different types of iterators.

Which STL is faster?

For searching a particular value, with std::set and std::map it takes O(log N) time, while with the other two it takes O(N) time; So, std::set or std::map are probably better. Since you have access to C++0x, you could also use std::unordered_set or std::unordered_map which take constant time on average.


1 Answers

There is a nice blog-post by Herb Sutter discussing the question. The gist is that adding container-based overloads for algorithms can create ambiguities if an overload for that algorithm with the same number of template-parameters already exists. Concepts were intended to fix that problem.

like image 54
Björn Pollex Avatar answered Nov 21 '22 11:11

Björn Pollex