Boost logo

Boost :

From: Thorsten Ottosen (nesotto_at_[hidden])
Date: 2004-09-11 16:54:02


"Neal Becker" <ndbecker2_at_[hidden]> wrote in message
news:chvoo4$qbl$1_at_sea.gmane.org...
| It is not unusual to need to construct a container from another where one or
| the other doesn't comply with sequence.
|
| When generic code needs to convert to a container of unknown type (e.g., the
| return container type of a function is a template parameter), a generic
| constructor is useful, and could be part of range. Here is the idea:
|
| #ifndef const_from_range_hpp
| #define const_from_range_hpp
|
| #include <boost/range.hpp>
| #include <algorithm>
| #include <boost/numeric/ublas/vector.hpp>
|
| namespace detail {
| template<typename cont_t, typename range>
| struct construct_from_range_impl {
| cont_t operator() (range const& r) {
| return cont_t (boost::begin (r), boost::end (r));
| }
| };

Please see the "Utility" section of the docs. You would be looking for
copy_range().

| template<typename T, typename range>
| struct construct_from_range_impl<boost::numeric::ublas::vector<T>, range>
| {
| typedef typename boost::numeric::ublas::vector<T> ret_t;
| ret_t operator() (range const& r) {
| ret_t v (boost::size (r));
| std::copy (boost::begin (r), boost::end (r), v.begin());

this can write past the end

| return v;
| }
| };
| }

hm..if ublas support is needed one could either do

1. give ublas classes a constructor that takes two iterators
2. overload copy_range

I would prefer 1.

br

Thorsten


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk