473,396 Members | 2,004 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

for_each with vector of function objects

I have a vector of N values and a vector of N functors. I want to apply
each functor to its corresponding value. What would be the 'correct'
STL way to do this.
for_each takes on functor and applies it to every element.
So presumably transform is a better option but it also takes one
functor.
This is obviously easily solved by a variety of methods but I am
interested in how the STL would best solve this problem.
e.g.

// fill vector with some values
vector<intx (values.begin(), values.end());

vector<functorfuncs;
// fill funcs with different functors, or functors with different
parameters

// would like to do STL equivalent of
for (int i=0; i<x.size(); ++i) {
funcs[i](x[i]);
}

Nov 14 '06 #1
6 1809
al*************@gmail.com wrote:
I have a vector of N values and a vector of N functors. I want to apply
each functor to its corresponding value. What would be the 'correct'
STL way to do this.
for_each takes on functor and applies it to every element.
So presumably transform is a better option but it also takes one
functor.
This is obviously easily solved by a variety of methods but I am
interested in how the STL would best solve this problem.
e.g.

// fill vector with some values
vector<intx (values.begin(), values.end());

vector<functorfuncs;
// fill funcs with different functors, or functors with different
parameters

// would like to do STL equivalent of
for (int i=0; i<x.size(); ++i) {
funcs[i](x[i]);
}
I'd write the explicit loop. But if you want to write a template that
takes iterators, it would be something like this:

template <class Iter1, class Iter2>
void apply(Iter1 first1, Iter1 last1, Iter2 first2, Iter2 last2)
{
while (first1 != last1 && first2 != last2)
(*first1++)(*first2++);
}

and you'd call it like this:

apply(funcs.begin(), funcs.end(), x.begin(), x.end());

Or, if the only reason you copied values in the first place was to get
the contents into a vector, you can skip that copy and pass the values
iterators:

apply(funcs.begin(), funcs.end(), values.begin(), values.end());

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Nov 14 '06 #2
In article <11**********************@h48g2000cwc.googlegroups .com>,
al*************@gmail.com wrote:
I have a vector of N values and a vector of N functors. I want to apply
each functor to its corresponding value. What would be the 'correct'
STL way to do this.
for_each takes on functor and applies it to every element.
So presumably transform is a better option but it also takes one
functor.
This is obviously easily solved by a variety of methods but I am
interested in how the STL would best solve this problem.
e.g.

// fill vector with some values
vector<intx (values.begin(), values.end());

vector<functorfuncs;
// fill funcs with different functors, or functors with different
parameters

// would like to do STL equivalent of
for (int i=0; i<x.size(); ++i) {
funcs[i](x[i]);
}
There is a transform that takes iterators into two containers, but it
assumes that the functions will produce outputs and you don't seem to be
doing that. As such, I agree with Pete's post.

--
To send me email, put "sheltie" in the subject.
Nov 14 '06 #3

Pete Becker wrote:
I'd write the explicit loop. But if you want to write a template that
takes iterators, it would be something like this:

template <class Iter1, class Iter2>
void apply(Iter1 first1, Iter1 last1, Iter2 first2, Iter2 last2)
{
while (first1 != last1 && first2 != last2)
(*first1++)(*first2++);
}
Do you need to increment both? transform that takes 2 inputs only
increments one and relies on "UB" if the unchecked sequence is shorter
than the first. (It can be longer, it simply runs for the length of the
checked sequence).

Also although it can be written in one line, I think it's better to use
pre-increment because it avoids a copy of the iterator, and you don't
know how expensive that might be from a template. Although it is more
lines in your code, I don't see that it will generate more lines of
object code. So how about:

template< typename Iter1, typename Iter2 >
void apply( Iter1 first1, Iter1 last1, Iter2 first2 )
{
while ( first1 != last1 )
{
(*first1)(*first2);
++first1;
++first2;
}
}
and you'd call it like this:

apply(funcs.begin(), funcs.end(), x.begin(), x.end());
In my example no x.end(), just the first 3 parameters.

Nov 14 '06 #4

Daniel T. wrote:
>
There is a transform that takes iterators into two containers, but it
assumes that the functions will produce outputs and you don't seem to be
doing that. As such, I agree with Pete's post.
To use that you'd have to write a non-iterator for the output, and the
operator that takes the 2 parameters would get the functor to invoke on
the object. Easier to write the algorithm, but if we really want to use
transform:

iterator: all of the operators (++, *, =) will do nothing. operator=()
could be a template because it does nothing with its parameter anyway.
Not sure what you'd do with the typedefs if we really need them anyway.

operator:

struct call_func
{
template < typename F, typename P >
void operator() ( F func, P param )
{
func( param );
}
};

The final way is to use for_each and get the functor to store the
beginning of one sequence. Let's say it stores the functors. Again it
could store an iterator.

template < typename FuncIterator >
struct FunctorFunctor
{
private:
FuncIterator func_iter;

public:
explicit FunctorFunctor( FuncIterator fi ) : func_iter ( fi ) {}

template < typename T >
void operator() ( T t )
{
(*func_iter)( t );
++func_iter;
}
};

template< typename FuncIterator >
FunctorFunctor< FuncIterator functor_functor( FuncIterator fi )
{
return FunctorFunctor< FuncIterator >( fi );
}

Nov 14 '06 #5
Ok, the solution is to write my own STL like function. I like the apply
idea.

If I were to return values from the function and store these in a
result vector then I could use transform a la:

transform (values.begin(), values.end(), functors.begin(),
results.begin(), my_apply());

where:

struct my_apply
{
template< typename T, typename F >
T operator()(T& x, F& f) {
return f(x);
}
};

Does this work with boost::lambda?

transform (values.begin(), values.end(), functors.begin(),
results.begin(), _2(_1) );

Thanks,
Alan
On Nov 14, 4:00 pm, "Daniel T." <danie...@earthlink.netwrote:
In article <1163518005.185341.257...@h48g2000cwc.googlegroups .com>,

alan.patters...@gmail.com wrote:
I have a vector of N values and a vector of N functors. I want to apply
each functor to its corresponding value. What would be the 'correct'
STL way to do this.
for_each takes on functor and applies it to every element.
So presumably transform is a better option but it also takes one
functor.
This is obviously easily solved by a variety of methods but I am
interested in how the STL would best solve this problem.
e.g.
// fill vector with some values
vector<intx (values.begin(), values.end());
vector<functorfuncs;
// fill funcs with different functors, or functors with different
parameters
// would like to do STL equivalent of
for (int i=0; i<x.size(); ++i) {
funcs[i](x[i]);
}There is a transform that takes iterators into two containers, but it
assumes that the functions will produce outputs and you don't seem to be
doing that. As such, I agree with Pete's post.

--
To send me email, put "sheltie" in the subject.
Nov 14 '06 #6
Earl Purple wrote:
Pete Becker wrote:
>I'd write the explicit loop. But if you want to write a template that
takes iterators, it would be something like this:

template <class Iter1, class Iter2>
void apply(Iter1 first1, Iter1 last1, Iter2 first2, Iter2 last2)
{
while (first1 != last1 && first2 != last2)
(*first1++)(*first2++);
}

Do you need to increment both?
Yes, you need to increment both. If you like, you can skip the second
comparison, but the information is there, so why not use it?
transform that takes 2 inputs only
increments one and relies on "UB" if the unchecked sequence is shorter
than the first.
It doesn't rely on UB. In fact, I'm not even sure what that means. It
says that the behavior is undefined if the second sequence is too short.
>
Also although it can be written in one line, I think it's better to use
pre-increment because it avoids a copy of the iterator, and you don't
know how expensive that might be from a template. Although it is more
lines in your code, I don't see that it will generate more lines of
object code. So how about:

template< typename Iter1, typename Iter2 >
void apply( Iter1 first1, Iter1 last1, Iter2 first2 )
{
while ( first1 != last1 )
{
(*first1)(*first2);
++first1;
++first2;
}
}
Write it however you like.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Nov 14 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: John Black | last post by:
Hi, I am not familiar with for_each very well, suppoase I have a vector<pair<unsigned int, unsigned int> > vec1 and the contents are {<0x00000000, 0x000000FF>, <0x10000000, 0x2FFFFFFF>} what...
2
by: Eric Lilja | last post by:
Hello, I have a std::vector storing pointers to objects that are dynamically allocated. When I am done with the vector I want to delete all pointers. Can I use std::for_each() or something to do...
9
by: shaun | last post by:
I am working on code where I am handed a vector of pointers vector<T*> or a map<std::string, T*>, and I have to delete the objects and set the pointers to zero. I have been using a 'for' loop and...
6
by: Philip Potter | last post by:
Hello there, I'm reading about the std::for_each() function in TC++PL, 3rd Ed. It seems like a good idea, but in practice I can never see a way to bend it to my wishes without writing huge...
3
by: PolkaHead | last post by:
I was wondering if there's a way to traverse a two-dimensional vector (vector of vectors) with a nested for_each call. The code included traverses the "outer" vector with a for_each, than it...
9
by: Chris Roth | last post by:
I have a vector of vectors: vector< vector<double v; and have initialized it with: v( 5 ); as I know I will have 5 columns of data. At this point, I read text file data into each of the the...
3
by: Chris Roth | last post by:
I have a vector (v) containing objects of class C. class C { private: double d; public: void foo( B& b ); };
4
by: Kenneth Porter | last post by:
I'm trying to create a class that represents a bucket of locks, and I want to lock a set of objects specified by a container of pointers. I can't seem to find the right combination of function...
3
by: fgh.vbn.rty | last post by:
I'm having problems setting up the functors to use for_each. Here's an example: class A { public: void getName() { return name; } private: string name; };
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.