16

Sometimes I get tired of all this my_vector.begin(), my_vector.end() noise. Last year at boostcon, Andrei Alexandrescu's keynote speech was titled Iterators Must Go (video)

Is there any progress on introducing ranges into C++, so I can finally say std::sort(my_vector)?

12
  • 12
    @Mike: One can hope.
    – Fred Nurk
    Commented Nov 18, 2010 at 22:40
  • 1
    I too am hoping for ranges, one day... however I find the title of Alexandrescu's presentation a little too carried away. What should algorithm like lower_bound return if not an iterator ? Commented Nov 19, 2010 at 7:55
  • 1
    @FredOverflow: ah but that's not the same, especially when the equal range would be long, then lower_bound is faster to compute, and that is all that is needed to know where to insert if you don't care about order. Commented Nov 20, 2010 at 14:50
  • 1
    Ranges would be better than iterators. Ranges are basically iterators done a bit better. However, we certainly don't want BOTH, I am sure. Commented Jan 14, 2011 at 12:32
  • 1
    @MikeDeSimone not quite, but there is a definite march to make C++ better, more accessible, more expressive and easier to use. Since Python has many of the same goals, it's not surprising that they end up with some similarities... :) Commented Jul 29, 2012 at 10:42

3 Answers 3

6

Range in C++ still has an insufficient experience.
As current experimental implementation, there are Boost.Range 2.0 and Oven Range Library.

1
  • And every standard algorithm is wrapped in the boost lib, so at least boost::sort(my_vector) (if not std::sort) is present, not future.
    – rafak
    Commented Nov 20, 2010 at 12:19
2

As far as I know, no progress has been made toward that end.

2
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>

template< class Container >
void sort( Container& c ) { sort( c.begin(), c.end() ); }

int main()
{
    using namespace std;

    int const           data[]  = {3, 1, 4, 1, 5, 9, 2, 6, 5, 4};
    vector<int>         v( data, data + sizeof( data )/sizeof( *data ) );

    sort( v );
    copy( v.begin(), v.end(), ostream_iterator<int>( cout, " " ) );
}

Of course, replace member function calls begin and end with calls of startOf and endOf (your versions), at least until C++0x...

5
  • 1
    boost::begin and boost::end work until you have support for std::begin and std::end.
    – Fred Nurk
    Commented Nov 18, 2010 at 22:46
  • 2
    That don't answer the question, doesn't it?
    – Klaim
    Commented Nov 18, 2010 at 22:53
  • 1
    @Klaim: it answers one of the questions, the OP's expressed wish to be able to "finally say std::sort(my_vector)". Noah answered the main question. Anyway, don't spoil a good answer by insisting there has to be a question. Cheers & hth., Commented Nov 18, 2010 at 23:08
  • This answer is more interesting than a "yes/no" answer. So it's worth an upvote. kthx. Commented Nov 18, 2010 at 23:32
  • 1
    I don't agree because it's an obvious answer. The question is more about making this standard than providing a solution.
    – Klaim
    Commented Nov 18, 2010 at 23:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.