Suggestion for lib function: apply<>

R

red floyd

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;
}
 
J

Jonathan Turkanis

red floyd said:
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;
}

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
 
J

Jeff Schwab

red said:
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;
}

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

red floyd

Jeff said:
red said:
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;
}


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=().
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,883
Members
47,414
Latest member
djangoframe

Latest Threads

Top