By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,492 Members | 1,242 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,492 IT Pros & Developers. It's quick & easy.

Suggestion for lib function: apply<>

P: n/a

I have a suggestion for the standard library....

This is sort of a combination of std::transform() and std::for_each().
It applies the binary function to each iterator in [start1, end1), to the
corresponding member of the container beginning with start2. Like for_each(),
it returns the functor.

---
template<typename Iter1, typename InIter2, typename BinaryFunc>
BinaryFunc apply(Iter1 start1, Iter1 end1, InIter2 start2, BinaryFunc func)
{
while (start1 != end1)
{
func(*start1, *start2);
++start1;
++start2;
}
return func;
}
---
You could dummy up something like this with for_each() and a clever functor, but
you begin to lose readability. Any comments, suggestions?
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:st*****@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]

Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"red floyd" <no*****@here.dude> wrote in message
news:c5*****************@newssvr29.news.prodigy.co m...

I have a suggestion for the standard library....

This is sort of a combination of std::transform() and std::for_each().
It applies the binary function to each iterator in [start1, end1), to the
corresponding member of the container beginning with start2. Like for_each(), it returns the functor.

---
template<typename Iter1, typename InIter2, typename BinaryFunc>
BinaryFunc apply(Iter1 start1, Iter1 end1, InIter2 start2, BinaryFunc func) {
while (start1 != end1)
{
func(*start1, *start2);
++start1;
++start2;
}
return func;
}
---
You could dummy up something like this with for_each() and a clever functor, but you begin to lose readability. Any comments, suggestions?


A number of algorithms in the standard library could be generalized in this
way, to operate on two sequences instead of one, using binary rather than
unary predicates or functions. For instance, one could have a version of
find_if which takes two sequences and a binary predicate.

Instead of introducing double versions of these algorithms, a more general
solution might be to define iterator adaptors which turn pairs of iterators
into iterators over pairs.

Jonathan
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:st*****@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]

Jul 22 '05 #2

P: n/a
red floyd wrote:

I have a suggestion for the standard library....

This is sort of a combination of std::transform() and std::for_each().
It applies the binary function to each iterator in [start1, end1), to the
corresponding member of the container beginning with start2. Like
for_each(), it returns the functor.

---
template<typename Iter1, typename InIter2, typename BinaryFunc>
BinaryFunc apply(Iter1 start1, Iter1 end1, InIter2 start2, BinaryFunc func)
{
while (start1 != end1)
{
func(*start1, *start2);
++start1;
++start2;
}
return func;
}
---
You could dummy up something like this with for_each() and a clever
functor, but
you begin to lose readability. Any comments, suggestions?


How is this different from std::transform, aside from the lack of an
output iterator?

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:st*****@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]

Jul 22 '05 #3

P: n/a
Jeff Schwab wrote:
red floyd wrote:

I have a suggestion for the standard library....

This is sort of a combination of std::transform() and std::for_each().
It applies the binary function to each iterator in [start1, end1), to the
corresponding member of the container beginning with start2. Like
for_each(), it returns the functor.

---
template<typename Iter1, typename InIter2, typename BinaryFunc>
BinaryFunc apply(Iter1 start1, Iter1 end1, InIter2 start2, BinaryFunc
func)
{
while (start1 != end1)
{
func(*start1, *start2);
++start1;
++start2;
}
return func;
}
---
You could dummy up something like this with for_each() and a clever
functor, but
you begin to lose readability. Any comments, suggestions?

How is this different from std::transform, aside from the lack of an
output iterator?


transform performs the operation and copies it to the output. the posted
function does the operation "in place", without the overhead of operator=().

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:st*****@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]

Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.