473,671 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Suggestion for lib function: apply<>


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<typena me 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
3 1680
"red floyd" <no*****@here.d ude> wrote in message
news:c5******** *********@newss vr29.news.prodi gy.com...

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<typena me 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
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<typena me 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
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<typena me 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1718
by: Richard Shea | last post by:
Hi - Is there a tool available which given a set of PHP scripts will analyse them and show you which function is called by which other function ? I'm looking for something which would allow me to see all the functions which call foo(). I feel like I've seen the output of something like this but I can't think where. thanks
3
10552
by: Nachi | last post by:
I am getting 2 resultsets depending on conditon, In the second conditon i am getting the above error could anyone help me.......... CREATE proc sp_count_AllNewsPapers @CustomerId int as declare @NewsId int set @NewsId = (select NewsDelId from NewsDelivery where CustomerId=@CustomerId )
2
3210
by: Eshrath | last post by:
Hi, What I am trying to do: ======================= I need to form a table in html using the xsl but the table that is formed is quite long and cannot be viewed in our application. So we are writing one object in C# which will take the entire table tag contents and renders. Ie., we need to pass "<table>………… <thead>……</thead>. <tr>.<td> <td>..<tr>.<td> <td> </table>" content to
17
2264
by: Albert Wagner | last post by:
I have a page with two frames. Frame1 contains an inline script. Frame2 contains inline and 2 loaded scripts. How may I call a function in frame1(inline) from frame2(inline or loaded)? Albert
3
1727
by: z. f. | last post by:
Hi, i'm using code in my aspx page. i have data binding where i use <%# Container.DataItem("DateStart") %> i also use code that makes a loop inside a regular <% %> block how can i pass data from the databinding to the loop that runs in a regular
9
3674
by: Simple Simon | last post by:
Java longs are 8 bytes. I have a Java long that is coming in from the network, and that represents milliseconds since Epoch (Jan 1 1970 00:00:00). I'm having trouble understanding how to get it into a struct timeval object. I get the ByteBuffer as an array of const unsigned char, 'buffer'. Here's an example: 00 00 01 0A 29 1D 07 E4 This value maps somehow to Thu Mar 23 16:57:49 2006 and some
14
3139
by: Michael | last post by:
Since the include function is called from within a PHP script, why does the included file have to identify itself as a PHP again by enclosing its code in <?php... <?> One would assume that the PHP interpreter works like any other, that is, it first expands all the include files, and then parses the resulting text. Can anyone help with an explanation? Thanks, M. McDonnell
2
2054
by: -Karl | last post by:
Couls someone please advise me on this error. What I am trying to do is be able to convert an XML document into arrays. I read that the subs & functions have to be in <scripttags. Thanks! Error: BC30456: 'Read_DOM' is not a member of
3
5637
by: Rune Allnor | last post by:
Hi folks. I have a function that takes an element in a vector as argument. The naive interface goes as float computeSomething(const std::vector<float>& v, size_t i) { size_t j = i-1; size_t k = i+1;
0
8907
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8817
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8593
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
7423
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
5687
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
4215
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
4396
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2804
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1799
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.