[range] Best way to construct a std::set from a range?
If I have a pair of iterators, I can construct a set like so: std::set<SomeType> s(it1, it2); If instead I have a range, I can adapt it like so: std::set<SomeType> s(boost::begin(range), boost::end(range)); However, my range object is being constructed on the fly. I'd like to be able to do: std::set<SomeType> s(MakeRange(args)); But clearly this is not supported by the library. It seems I need to store the range object in a variable in order to do this, which defeats the syntax-simplifying benefits of using ranges. Can this be avoided? Are there some recommended no-overhead tricks for doing this that achieve a clean syntax? Implicit conversions? Explicit "ToSet" conversion functions? Can boost.assign be of help? (My initial reading of the assign documentation suggests not, but perhaps I'm missing something). I'm also not averse to a macro so long as it looks "language-like," but so far I haven't produced anything satisfactory. Has anyone else wrestled with this one and come out victorious? Thanks, -Gabe
Gabriel Redner wrote:
If I have a pair of iterators, I can construct a set like so:
std::set<SomeType> s(it1, it2);
If instead I have a range, I can adapt it like so:
std::set<SomeType> s(boost::begin(range), boost::end(range));
However, my range object is being constructed on the fly. I'd like to be able to do:
std::set<SomeType> s(MakeRange(args));
what about template<typename Container> void assign_range(Container& c, const Range& r) { c.assign(begin(r), end(r)); } std::set<SomeType> s; assign_range(s, MakeRange(args));
Sure, that works - I was hoping for something that fits naturally on
one line, though.
Thanks,
-Gabe
On Wed, Mar 3, 2010 at 10:54 AM, Mathias Gaunard
Gabriel Redner wrote:
If I have a pair of iterators, I can construct a set like so:
std::set<SomeType> s(it1, it2);
If instead I have a range, I can adapt it like so:
std::set<SomeType> s(boost::begin(range), boost::end(range));
However, my range object is being constructed on the fly. I'd like to be able to do:
std::set<SomeType> s(MakeRange(args));
what about
template<typename Container> void assign_range(Container& c, const Range& r) { c.assign(begin(r), end(r)); }
std::set<SomeType> s; assign_range(s, MakeRange(args));
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
On Wed, Mar 3, 2010 at 11:43 AM, Gabriel Redner
Sure, that works - I was hoping for something that fits naturally on one line, though.
Well, you could use phoenix2 for a one-liner: assign(arg1, begin(arg2), end(arg2))(s, MakeRange(args))
Or you could do std::set<SomeType> s = boost::copy_range< std::set<SomeType> >( range );
-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users- bounces@lists.boost.org] On Behalf Of OvermindDL1 Sent: Donnerstag, 4. März 2010 06:38 To: boost-users@lists.boost.org Subject: Re: [Boost-users] [range] Best way to construct a std::set from arange?
On Wed, Mar 3, 2010 at 11:43 AM, Gabriel Redner
wrote: Sure, that works - I was hoping for something that fits naturally on one line, though.
Well, you could use phoenix2 for a one-liner: assign(arg1, begin(arg2), end(arg2))(s, MakeRange(args)) _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- Sebastian Theophil · stheophil@think-cell.com Software Engineer think-cell Software GmbH · Chausseestr. 8/E · 10115 Berlin, Germany http://www.think-cell.com · phone +49 30 666473-10 · toll-free (US) +1 800 891 8091 Directors: Dr. Markus Hannebauer, Dr. Arno Schoedl · Amtsgericht Berlin-Charlottenburg, HRB 85229
Sebastian Theophil wrote:
Or you could do
std::set<SomeType> s = boost::copy_range< std::set<SomeType> >( range );
Cool, where's boost::copy_range defined? This should be usable in an initializer list as well I presume. Jeff
Hi Jeff, I'm still using boost 1.35 and boost::copy_range is defined in boost\range\iterator_range.hpp Regards Sebastian
-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users- bounces@lists.boost.org] On Behalf Of Jeff Flinn Sent: Samstag, 6. März 2010 01:49 To: boost-users@lists.boost.org Subject: Re: [Boost-users] [range] Best way to construct a std::set fromarange?
Sebastian Theophil wrote:
Or you could do
std::set<SomeType> s = boost::copy_range< std::set<SomeType> >( range );
Cool, where's boost::copy_range defined? This should be usable in an initializer list as well I presume.
Jeff
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- Sebastian Theophil · stheophil@think-cell.com Software Engineer think-cell Software GmbH · Chausseestr. 8/E · 10115 Berlin, Germany http://www.think-cell.com · phone +49 30 666473-10 · toll-free (US) +1 800 891 8091 Directors: Dr. Markus Hannebauer, Dr. Arno Schoedl · Amtsgericht Berlin-Charlottenburg, HRB 85229
Gabriel Redner wrote:
If I have a pair of iterators, I can construct a set like so:
std::set<SomeType> s(it1, it2);
If instead I have a range, I can adapt it like so:
std::set<SomeType> s(boost::begin(range), boost::end(range));
However, my range object is being constructed on the fly. I'd like to be able to do:
std::set<SomeType> s(MakeRange(args));
Have you looked at range_ex? http://www.boostpro.com/vault/index.php?action=downloadfile&filename=range_ex.zip&directory=Algorithms& I think this has been reviewed, but I don't remember its current "official" status at the moment.
participants (6)
-
Gabriel Redner
-
Jeff Flinn
-
Mathias Gaunard
-
Nat Goodspeed
-
OvermindDL1
-
Sebastian Theophil