473,757 Members | 10,736 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

for_each

Is it wrong to describe for_each as a modifying algorithm? It is
described as one here: http://www.josuttis.com/libbook/algolist.pdf.
transform appears to be the algorithm to use for modifying elements in a
range.

Fraser.

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Sep 17 '06 #1
27 2235
"Fraser Ross" <fraserATmember s.v21.co.ukwrot e:
Is it wrong to describe for_each as a modifying algorithm? It is
described as one here: http://www.josuttis.com/libbook/algolist.pdf.
transform appears to be the algorithm to use for modifying elements in a
range.
for_each is a non-modifying algorithm does not explicitly modify the
contents of the container. The functor passed into for_each might, but
the algorithm doesn't. Specifically, there is no assignment operation
inside the algorithm.

--
There are two things that simply cannot be doubted. Logic and our
ability to sense the world around us. Doubt those, and you no longer
have anyone to discuss it with, nor any ability to discuss it.
Sep 17 '06 #2

"Daniel T."
for_each is a non-modifying algorithm does not explicitly modify the
contents of the container. The functor passed into for_each might, but
the algorithm doesn't.
I think the intention is that although the algorithm doesn't prevent
modification it is expected anyway that the functor does not modify
elements.

Fraser.

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Sep 17 '06 #3
In article <11************ **@sp6iad.super feed.net>, "Fraser Ross"
<fraserATmember s.v21.co.uksays ...
Is it wrong to describe for_each as a modifying algorithm? It is
described as one here: http://www.josuttis.com/libbook/algolist.pdf.
transform appears to be the algorithm to use for modifying elements in a
range.
The C++ standard places the description of for_each in the "Non-
modifying Sequence Operations." OTOH, there's nothing in its description
that precludes it from modifying the items in the sequence either.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Sep 17 '06 #4
In article <11************ **@sp6iad.super feed.net>, "Fraser Ross"
<fraserATmember s.v21.co.uksays ...
>
"Daniel T."
for_each is a non-modifying algorithm does not explicitly modify the
contents of the container. The functor passed into for_each might, but
the algorithm doesn't.

I think the intention is that although the algorithm doesn't prevent
modification it is expected anyway that the functor does not modify
elements.
I doubt it. Other algorithms (e.g. transform) state specific
requirements about lack of side-effects in the operations they invoke. I
think if that was intended, it would be stated in the dsecription of
for_each as well.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Sep 17 '06 #5
"Fraser Ross" <fraserATmember s.v21.co.ukwrot e:
>
"Daniel T."
>for_each is a non-modifying algorithm does not explicitly modify the
contents of the container. The functor passed into for_each might, but
the algorithm doesn't.

I think the intention is that although the algorithm doesn't prevent
modification it is expected anyway that the functor does not modify
elements.
That intention is not stated in the standard. The description of for_each is
very clear:

template<class InputIterator, class Function>
Function for_each(InputI terator first, InputIterator last, Function f);

Effects: Applies f to the result of dereferencing every iterator in the
range [first, last), starting from first and proceeding to last - 1.
Returns: f.
Complexity: Applies f exactly last - first times.

It follows that if you pass a const_iterator, you will not be able to
modify. If you pass a (non-const) iterator, you will be able to modify
because the result of dereferencing the iterator is not const.
Best

Kai-Uwe Bux
Sep 17 '06 #6
The book "STL Tutorial And Reference Guide" says "It is assumed f does
not apply any nonconstant function through the dereferenced iterator."

The help files with BCB6 says "Since this a non-mutating algorithm, the
function f cannot make any modifications to the sequence, but it can
achieve results through side effects (such as copying or printing). "

They seem to expecting the range to not be modified.

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Sep 17 '06 #7
"Fraser Ross" <fraserATmember s.v21.co.ukwrot e:
The book "STL Tutorial And Reference Guide" says "It is assumed f does
not apply any nonconstant function through the dereferenced iterator."

The help files with BCB6 says "Since this a non-mutating algorithm, the
function f cannot make any modifications to the sequence, but it can
achieve results through side effects (such as copying or printing). "

They seem to expecting the range to not be modified.
As I already pointed out, those opinions have, to the best of my knowledge,
no basis in the language of the standard. Maybe you should ask the authors
of your sources, where in the standard they find the provisions in support
for their theory.
Best

Kai-Uwe Bux

Sep 17 '06 #8
Fraser Ross wrote:
The book "STL Tutorial And Reference Guide" says "It is assumed f does
not apply any nonconstant function through the dereferenced iterator."

The help files with BCB6 says "Since this a non-mutating algorithm, the
function f cannot make any modifications to the sequence, but it can
achieve results through side effects (such as copying or printing). "

They seem to expecting the range to not be modified.
On the other hand, if you look at the page cited in the original
message, it lists for_each as both a non-modifying algorithm and a
modifying algorithm.

Much of this confusion comes from failing to distinguish between two
different modifications: modifying the order of the elements in the
passed sequence (e.g. sort) and modifying elements in the sequence (e.g.
transform).

In fact, the classification in the standard distinguishes between
modifying sequence operations and non-modifying sequence operations. The
choice of words is deliberate: it emphasizes that what's being operated
on is the sequence, and not the elements themselves. So for_each is a
non-modifying sequence operation, because it does not modify the order
the elements in the sequence.

There's a newly added note that says that if you pass a mutable iterator
to for_each, the function can modify the elements.

--

-- Pete

Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." For more information about this book, see
www.petebecker.com/tr1book.
Sep 17 '06 #9
In article <11************ **@sp6iad.super feed.net>,
"Fraser Ross" <fraserATmember s.v21.co.ukwrot e:
The book "STL Tutorial And Reference Guide" says "It is assumed f does
not apply any nonconstant function through the dereferenced iterator."

The help files with BCB6 says "Since this a non-mutating algorithm, the
function f cannot make any modifications to the sequence, but it can
achieve results through side effects (such as copying or printing). "

They seem to expecting the range to not be modified.
From Stroustrup:

The for_each() algorithm is classified as nonmodifying because it
doesn't explicitly modify a sequence. However, if applied to a
non-const sequence for_each()'s operation (its third argument) may
change the elements of the sequence.

--
There are two things that simply cannot be doubted. Logic and our
ability to sense the world around us. Doubt those, and you no longer
have anyone to discuss it with, nor any ability to discuss it.
Sep 17 '06 #10

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

Similar topics

5
4678
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
1499
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
1550
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:
1
1877
by: Capstar | last post by:
Hi NG, I have a question on std::for_each. I try to use this in combination with std::bind2nd to call a method of all functions in a container (std::set or std::map) and pass that method the second argument of std::bind2nd. But for some reason the compiler wants met ot have all const objects, which obviously doesn't work when the method is non-const. example:
12
2618
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)
9
1846
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 thought it might be instructive to write a 'deletePointer' which can be used in an algorithm or standalone. (code at end of mail) I discovered I could not simply for_each(v.begin(),v.end(),deletePointer);
3
2886
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(),
9
2605
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 vectors using push_back. I know that I will be reading in 5000 elements into each vector, so I use reserve: ifstream f( "file.txt" ); if(f.is_open()) {
13
4417
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.
9
4011
by: nguillot | last post by:
Hello I used to loop on a std::map<k, dto act on the data (d) like that (d being a class with setIntMember method): typedef std::map<k, dtMap; struct setIntMember { setIntMember(int j) : i(j) {}
0
9489
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
10072
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
9906
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
9885
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
9737
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...
1
7286
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5172
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...
1
3829
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
3
2698
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.