> Subject: [Boost-users] adding elements to a std::container in a single
> expression
> Message-ID: <j2787j$bj9$1@dough.gmane.org>
> 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 )
);