473,657 Members | 2,413 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(v ec5),
op
)//where op is a 4-ary function

Sep 27 '07 #1
5 1569
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(v ec5),
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(v ec5),
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.d udewrote:
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(v ec5),
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.goo glegroups.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(v ec5),
op
)//where op is a 4-ary function
See http://www.boost.org/libs/iterator/d..._iterator.html. Quote:

std::vector<dou ble>::const_ite rator beg1 = vect_of_doubles .begin();
std::vector<dou ble>::const_ite rator end1 = vect_of_doubles .end();
std::vector<int >::const_iterat or beg2 = vect_of_ints.be gin();
std::vector<int >::const_iterat or end2 = vect_of_ints.en d();

std::for_each(b eg1, end1, func_0());
std::for_each(b eg2, 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_tup le(beg1, beg2)
),
boost::make_zip _iterator(
boost::make_tup le(end1, end2)
),
zip_func()
);
struct zip_func :
public std::unary_func tion<const boost::tuple<co nst double&, const int&>&,
void>
{
void operator()(cons t boost::tuple<co nst 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...@plea se.comwrote:
"er" <erwann.rog...@ gmail.comwrote in message

news:11******** **************@ n39g2000hsh.goo glegroups.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(v ec5),
op
)//where op is a 4-ary function

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

std::vector<dou ble>::const_ite rator beg1 = vect_of_doubles .begin();
std::vector<dou ble>::const_ite rator end1 = vect_of_doubles .end();
std::vector<int >::const_iterat or beg2 = vect_of_ints.be gin();
std::vector<int >::const_iterat or end2 = vect_of_ints.en d();

std::for_each(b eg1, end1, func_0());
std::for_each(b eg2, 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_tup le(beg1, beg2)
),
boost::make_zip _iterator(
boost::make_tup le(end1, end2)
),
zip_func()
);
struct zip_func :
public std::unary_func tion<const boost::tuple<co nst double&, const int&>&,
void>
{
void operator()(cons t boost::tuple<co nst 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
3836
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 and a single number (e.g. multiply all the values in a vector by 3 or so). So, this is my intent: #ifndef ALGORITHM_EXT_HH #define ALGORITHM_EXT_HH
5
4670
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 non-const reference? Here is some program which demonstrates probable necessity in such forms of for_each() and transform().
3
1491
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
1542
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 an operator() class to be properly executed inside for_each()? Maybe example will be more accurate:
12
2606
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()); This seems inefficient so I rewrite it as: int n = v.size(); for (int i = 0; i < n; ++i)
2
1504
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 tasks, but I end up using std::for_each Is a special/custom iterator the only way to do that with std::transform?
3
2864
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 relies on the PrintFunctor to traverse the "inner" vector with another for_each. Is it possible to nest the second for_each, so I don't have to include a for_each in my function object. Is it possible to do a: for_each(myVec.begin(), myVec.end(),
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 = 7 7 8 8 9 9 the slice at position 3 would be 3 8 (v1(3) and v2(3)). I think it is possible to use transform to make the slice matrix, but I'm having trouble with the final argument:
13
4411
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 the same.
0
8402
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8315
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8508
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8608
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7341
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5633
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4164
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4323
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1627
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.