On 27.03.2016 06:06, Stefan Ram wrote:
>
r...@zedat.fu-berlin.de (Stefan Ram) writes:
>> An algorithm takes two iterators as its first two arguments,
>> so I have to encode N into two iterators in main, and then
>> call my custom algorithm with these two iterators.
>> { int N; ::std::cin >> N; ::std::vector< int >v( N );
>> algorithm( v.begin(), v.end() ); }
>
> You might think that it wastes resources to create a vector
> of N ints just to encode the number N. This remembers me of
> a part of an interview with Alexander Stepanov from 1998
> where he said:
>
> »It would be much nicer if begin and end were global - it
> would allow us to define them for C arrays. It would be
> so much nicer if operator* was global with the default
> definitions:
>
> template <class T> T& operator*(T& x) { return x;}
>
> template <class T> const T& operator*(const T& x) { return x;}
>
> It would allow us to write:
>
> copy(0, 25, ostream_iterator<int>("\n"));«.
>
> In the meantime, the global »begin« and »end« became a
> reality, even »cbegin« and »cend«. And today, they are
> recommended instead of the element functions. Maybe one
> day we will even get the global dereference operators that
> dereferences non-iterators to themselves!
Oh thanks. I've always attributed this idea to Dietmar Kuehl. Well, at
least the idea of the triad of functions begin(), end() and (c++17)
size(), ever since I failed to attribute it at all in a blog posting
long ago, and tried to make up for it.
Hrmf.
So, it was Stepanov, I should have guessed. :)
Cheers!,
- Alf
PS: Now that C++ is in the process of gaining a global size() function,
I have changed my view of it. I now think a size() function is *wrong*,
because it conflates two or three logical functions. Namely,
static_size() for arrays, n_items() for a dynamic size collection, and
length() for a string, say. In particular n_items() does not naturally
map to the size() member function of std::bitset, but rather to its
dynamic count(), which corresponds to the dynamic .size() of a std::set.