Boost logo

Boost Users :

Subject: Re: [Boost-users] adding elements to a std::container in a
From: Martin Elzen (martinelzen_at_[hidden])
Date: 2011-08-14 09:48:46


> Subject: [Boost-users] adding elements to a std::container in a single
> expression
> Message-ID: <j2787j$bj9$1_at_[hidden]>
> Content-Type: text/plain; format=flowed; charset="iso-8859-1";
> reply-type=original
>
> until now I've usually written my own template to do this:
>
>
> template<typename T>
> struct push_back_container:T
> { push_back_container &_push_back(const typename T::value_type &_r)
> { push_back(_r);
> return *this;
> }
> };
>
> one can use this like this:
>
> push_back_container<std::vector<something>
> >()._push_back(something1)._push_back(_something2)
>
> Is there some more elegant way to do this?

Been there, and had the same issues. I've always wondered why std::vector gives you so many ways to assign values to it, but so few to append items, as personally I want to append items much more often than totally change a vector's contents. For what you want to do there is also a standard assign-with-back-inserter whatchamacallit :

include vector
include algorithm // assign
include iterator // back_inserter

std::copy(
   source.begin(), source.end(),
   std::back_inserter( targetSequenceWithpush_back_memberFn )
);

                                               



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