Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the status of ranges in C++?

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)?

like image 634
fredoverflow Avatar asked Nov 18 '10 22:11

fredoverflow


People also ask

Is there Range function in C?

Using range in switch case in C/C++ You all are familiar with switch case in C/C++, but did you know you can use range of numbers instead of a single number or character in case statement.

How do you check if a number is within a range?

If x is in range, then it must be greater than or equal to low, i.e., (x-low) >= 0. And must be smaller than or equal to high i.e., (high – x) <= 0. So if result of the multiplication is less than or equal to 0, then x is in range.


3 Answers

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

like image 194
Akira Takahashi Avatar answered Oct 08 '22 19:10

Akira Takahashi


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

like image 24
Edward Strange Avatar answered Oct 08 '22 19:10

Edward Strange


#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...

like image 30
Cheers and hth. - Alf Avatar answered Oct 08 '22 19:10

Cheers and hth. - Alf