
I'm always writing (e.g.) std::vector<double> out (size); std::transform (in.begin(), in.end(), out.begin(), func); Wouldn't it be nice to have versions of the std algorithms that worked like: template<typename out_cont_t, typename in_cont_t, typename UnaryOperation> inline out_cont_t transform_r (const in_cont_t& in, UnaryOperation u) { out_cont_t out (boost::size (in)); std::transform (boost::begin (in), boost::end (in), boost::begin (out), u); return out; } Then you can say: std::vector<double> out = transform_r<std::vector<double> > (in, func); That is, the first template parameter is the desired return container type, and the algorithm constructs the return value into it. Since constructors can be elided, I think this may be just as efficient as the original, or maybe better. OK, perhaps it looks like I didn't save much typing, but when you chain function calls you save a lot.

At Friday 2004-10-29 11:34, you wrote:
I'm always writing (e.g.)
std::vector<double> out (size);
std::vector<double> out(in.size()); // isn't this what you mean?
std::transform (in.begin(), in.end(), out.begin(), func);
Wouldn't it be nice to have versions of the std algorithms that worked like:
template<typename out_cont_t, typename in_cont_t, typename UnaryOperation> inline out_cont_t transform_r (const in_cont_t& in, UnaryOperation u) { out_cont_t out (boost::size (in)); std::transform (boost::begin (in), boost::end (in), boost::begin (out), u); return out; }
Then you can say:
std::vector<double> out = transform_r<std::vector<double> > (in, func);
it has been a source of astonishment to me since I first learned the STL that the concept of "range" was never formalized into an object. containers then could have "free" conversions to range (.begin(), .end()) .. and the algorithms would take either a range or two iterators defining it.
That is, the first template parameter is the desired return container type, and the algorithm constructs the return value into it.
Since constructors can be elided, I think this may be just as efficient as the original, or maybe better.
OK, perhaps it looks like I didn't save much typing, but when you chain function calls you save a lot.
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Victor A. Wagner Jr. http://rudbek.com The five most dangerous words in the English language: "There oughta be a law"

"Neal D. Becker" <ndbecker2@verizon.net> writes:
I'm always writing (e.g.)
std::vector<double> out (size); std::transform (in.begin(), in.end(), out.begin(), func);
Wouldn't it be nice to have versions of the std algorithms that worked like:
template<typename out_cont_t, typename in_cont_t, typename UnaryOperation> inline out_cont_t transform_r (const in_cont_t& in, UnaryOperation u) { out_cont_t out (boost::size (in)); std::transform (boost::begin (in), boost::end (in), boost::begin (out), u); return out; }
Then you can say:
std::vector<double> out = transform_r<std::vector<double> > (in, func);
Well, this limits your transformation to only work on the entire range of the input. I'm also concerned about the overhead of the temporary.
Since constructors can be elided, I think this may be just as efficient as the original, or maybe better.
"Can be" and "may be" doesn't mean "will be."
OK, perhaps it looks like I didn't save much typing, but when you chain function calls you save a lot.
Is the slight savings in typing worth the added restrictions and potential cost? I'm not inclined to think so. Too many simple wrappers that barely improve things leads to a proliferation of similar interfaces that become hard to learn due to the numerous and subtle differences between them. I like to see enough improvement to justify the learning curve, etc. -- Chris

On Fri, 29 Oct 2004 15:53:56 -0400, Chris Uzdavinis <chris@uzdavinis.com> wrote:
Well, this limits your transformation to only work on the entire range of the input.
Not quite so. Think about ranges (boost::iterator_range<>), that have a container like interface (begin()/end()) - you can always get a range from a container as a single object. -- Maxim Yegorushkin

Neal D. Becker wrote:
I'm always writing (e.g.)
std::vector<double> out (size); std::transform (in.begin(), in.end(), out.begin(), func);
Wouldn't it be nice to have versions of the std algorithms that worked like:
template<typename out_cont_t, typename in_cont_t, typename UnaryOperation> inline out_cont_t transform_r (const in_cont_t& in, UnaryOperation u) { out_cont_t out (boost::size (in)); std::transform (boost::begin (in), boost::end (in), boost::begin (out), u); return out; }
The RangeLib does just that. An article will appear on CUJ Dec' 04. http://www.torjo.com/rangelib/ I should be able to generate some documentation by early next week. Until then, you can contact me privately - if you have questions. Best, John -- John Torjo, Contributing editor, C/C++ Users Journal -- "Win32 GUI Generics" -- generics & GUI do mix, after all -- http://www.torjo.com/win32gui/ -- v1.5 - tooltips at your fingertips (work for menus too!) + bitmap buttons (work for MessageBox too!) + tab dialogs, hyper links, lite html
participants (5)
-
Chris Uzdavinis
-
John Torjo
-
Maxim Yegorushkin
-
Neal D. Becker
-
Victor A. Wagner Jr.