Boost logo

Boost Users :

Subject: Re: [Boost-users] [range] What's the best way to initialize a new container from a range?
From: Darren Garvey (darren.garvey_at_[hidden])
Date: 2011-08-29 08:41:43


Hi Martin,

On 29 August 2011 13:15, Martin B. <0xCDCDCDCD_at_[hidden]> wrote:

> On 26.08.2011 20:21, Ovanes Markarian wrote:
>
>> On Fri, Aug 26, 2011 at 2:30 PM, Martin B. <0xCDCDCDCD_at_[hidden]
>> <mailto:0xCDCDCDCD_at_[hidden]>> wrote:
>>
>> Say I have a range R and I want to construct a new container from
>> the range R. Will I always have to repeat the expression yielding
>> the range, or is there a shorter way?
>>
>> Example:
>>
>> std::vector<int> numbers(
>> boost::irange(7, 42).begin(),
>> boost::irange(7, 42).end()
>> );
>>
>> What about that:
>> ...
>>
>> integer_range<int> ir=irange(7,42);
>> vector<int> numbers(ir.begin(), ir.end());
>>
>>
> For this to work I need the exact type of the range, which can be quite
> annoying as far as I could tell. (Plus, I *don't want* to care what type of
> the range is.)
>
> Really, if I had C++11/auto, I wouldn't mind so much, i.e.
>
> auto xr = get_some_range(...);
> vector<int> numbers(xr.begin(), xr.end());
>
> but I don't have an `auto` capable compiler, so spelling out the range type
> for this is really crappy.
>

If you don't care about the type of range, you could always try:

BOOST_AUTO( xr, get_some_range(...) );
vector<int> numbers( xr.begin(), xr.end() );

http://www.boost.org/doc/libs/1_47_0/doc/html/typeof/refe.html#typeof.auto

Cheers,
Darren



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net