473,503 Members | 2,136 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

is anyone aware of generalized transform, for_each?

er
hi,

is anyone aware of a library that generalizes transform, for_each?
e.g.

transform(
vec1.begin(),
vec1.end(),
vec2.begin(),
vec3.begin(),
vec4.begin(),
back_inserter(vec5),
op
)//where op is a 4-ary function

Sep 27 '07 #1
5 1559
er wrote:
hi,

is anyone aware of a library that generalizes transform, for_each?
e.g.

transform(
vec1.begin(),
vec1.end(),
vec2.begin(),
vec3.begin(),
vec4.begin(),
back_inserter(vec5),
op
)//where op is a 4-ary function
I've just written one,

template <class II, class OI, class F4>
OI transform(II first, II last, II first2, II first3, II first4, OI
dest, F4 func)
{
while (first != last)
{
*dest = func(*first, *first2, *first3, *first4);
++first;
++first2;
++first3;
++first4;
++dest;
}
return dest;
}

That will be $8000 please.

john
Sep 27 '07 #2
John Harrison wrote:
er wrote:
>hi,

is anyone aware of a library that generalizes transform, for_each?
e.g.

transform(
vec1.begin(),
vec1.end(),
vec2.begin(),
vec3.begin(),
vec4.begin(),
back_inserter(vec5),
op
)//where op is a 4-ary function

I've just written one,

template <class II, class OI, class F4>
OI transform(II first, II last, II first2, II first3, II first4, OI
dest, F4 func)
template <typename II1, typename II2, typename II3, typename II4,
typename OI, typename F4>
OI transform(II1 first, II1 last, II2 first2, II3 first3, II4 first4,
OI dest, F4 func)
{
while (first != last)
{
*dest = func(*first, *first2, *first3, *first4);
++first;
++first2;
++first3;
++first4;
++dest;
}
return dest;
}
Which allows for merging of different iterator types (list, vector,
set...) as well as different types for the values (int, float, double...)
Sep 27 '07 #3
er
On Sep 27, 12:13 pm, red floyd <no.s...@here.dudewrote:
John Harrison wrote:
er wrote:
hi,
is anyone aware of a library that generalizes transform, for_each?
e.g.
transform(
vec1.begin(),
vec1.end(),
vec2.begin(),
vec3.begin(),
vec4.begin(),
back_inserter(vec5),
op
)//where op is a 4-ary function
I've just written one,
template <class II, class OI, class F4>
OI transform(II first, II last, II first2, II first3, II first4, OI
dest, F4 func)

template <typename II1, typename II2, typename II3, typename II4,
typename OI, typename F4>
OI transform(II1 first, II1 last, II2 first2, II3 first3, II4 first4,
OI dest, F4 func)
{
while (first != last)
{
*dest = func(*first, *first2, *first3, *first4);
++first;
++first2;
++first3;
++first4;
++dest;
}
return dest;
}

Which allows for merging of different iterator types (list, vector,
set...) as well as different types for the values (int, float, double...)
yes, thank you both, but this is generalized in type, not in
dimension. i should have made that clear in the first post, and
replace 4 by an arbitrary N. i'm aware that this is no trivial
task...hence my posting here.

Sep 27 '07 #4

"er" <er***********@gmail.comwrote in message
news:11**********************@n39g2000hsh.googlegr oups.com...
hi,

is anyone aware of a library that generalizes transform, for_each?
e.g.

transform(
vec1.begin(),
vec1.end(),
vec2.begin(),
vec3.begin(),
vec4.begin(),
back_inserter(vec5),
op
)//where op is a 4-ary function
See http://www.boost.org/libs/iterator/d..._iterator.html. Quote:

std::vector<double>::const_iterator beg1 = vect_of_doubles.begin();
std::vector<double>::const_iterator end1 = vect_of_doubles.end();
std::vector<int>::const_iterator beg2 = vect_of_ints.begin();
std::vector<int>::const_iterator end2 = vect_of_ints.end();

std::for_each(beg1, end1, func_0());
std::for_each(beg2, end2, func_1());
These two iterations can now be replaced with a single one as follows:

std::for_each(
boost::make_zip_iterator(
boost::make_tuple(beg1, beg2)
),
boost::make_zip_iterator(
boost::make_tuple(end1, end2)
),
zip_func()
);
struct zip_func :
public std::unary_function<const boost::tuple<const double&, const int&>&,
void>
{
void operator()(const boost::tuple<const double&, const int&>& t) const
{
m_f0(t.get<0>());
m_f1(t.get<1>());
}

private:
func_0 m_f0;
func_1 m_f1;
};
Jeff Flinn
Sep 28 '07 #5
er
On Sep 27, 7:59 pm, "Jeff F" <norepl...@please.comwrote:
"er" <erwann.rog...@gmail.comwrote in message

news:11**********************@n39g2000hsh.googlegr oups.com...
hi,
is anyone aware of a library that generalizes transform, for_each?
e.g.
transform(
vec1.begin(),
vec1.end(),
vec2.begin(),
vec3.begin(),
vec4.begin(),
back_inserter(vec5),
op
)//where op is a 4-ary function

Seehttp://www.boost.org/libs/iterator/doc/zip_iterator.html. Quote:

std::vector<double>::const_iterator beg1 = vect_of_doubles.begin();
std::vector<double>::const_iterator end1 = vect_of_doubles.end();
std::vector<int>::const_iterator beg2 = vect_of_ints.begin();
std::vector<int>::const_iterator end2 = vect_of_ints.end();

std::for_each(beg1, end1, func_0());
std::for_each(beg2, end2, func_1());
These two iterations can now be replaced with a single one as follows:

std::for_each(
boost::make_zip_iterator(
boost::make_tuple(beg1, beg2)
),
boost::make_zip_iterator(
boost::make_tuple(end1, end2)
),
zip_func()
);
struct zip_func :
public std::unary_function<const boost::tuple<const double&, const int&>&,
void>
{
void operator()(const boost::tuple<const double&, const int&>& t) const
{
m_f0(t.get<0>());
m_f1(t.get<1>());
}

private:
func_0 m_f0;
func_1 m_f1;};

Jeff Flinn
thanks!

Sep 28 '07 #6

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

Similar topics

20
3809
by: Steffen Brinkmann | last post by:
Hi! I tried to modify the transform algorithm in a way that it doesn't take iterators, but a reference to a container class and a value, because Mostly I need to do an operation of a container...
5
4654
by: Alex Vinokur | last post by:
Functor-parameters in the for_each() and transform() algorithms are passed by value. Might it make sense to have also algorithms for_each2() and transform2() which pass Functor-parameters by...
3
1484
by: Griff | last post by:
#include <iostream> using namespace std; #include <vector> #include <string> #include <fstream> #include <algorithm> template<class C>void PrintAll(C&v) {
6
1536
by: Michal Wyrebski | last post by:
Hello, I'm new in this group and I don't know if my questions are too silly, but I'm intermediate programmer and don't have enought experience. Please be charitable. I don't know how to write...
12
2594
by: sashan | last post by:
Sometimes I write code using std::for_each. Sometimes I use it on several container classes that are the same size. for_each(v.begin(), v.end(), f1()); for_each(u.begin(), u.end(), f2()); ...
2
1494
by: Diego Martins | last post by:
Hi! Often, I have some situations when I have to convert a vector of 10 elements to a vector of 30 elements or vice-versa. (the ratio is 1-3) I want to use the std::transform do to these...
3
2854
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...
6
338
by: Chris Roth | last post by:
I'm just starting out using the algorithms in c++ std library. I have a vector of vectors (v) and want to get a slice at some index. ex. If v held two vectors, v1 and v2: v1 = 1 2 3 4 5 6 v2 =...
13
4397
by: Sarath | last post by:
What's the advantage of using for_each than the normal iteration using for loop? Is there any 'tweak' to use stream objects as 'Function' parameter of for_each loop. Seems copy function can do...
0
7091
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
7282
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,...
1
6998
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...
0
7464
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5586
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5018
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3171
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
741
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
391
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.