By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,446 Members | 3,120 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,446 IT Pros & Developers. It's quick & easy.

Modifying the value of each element stored in a std::vector.

P: n/a
Look at my code:

void modify_element(BigType &x)
{
/* not shown */
}

BigType modify_element_copy(BigType x)
{
modify_element(x);
return x;
}

void modify_container(std::vector<BigType> &v)
{
std::transform(v.begin(), v.end(), v.begin(), modify_element_copy);
}

Can I replace the call to std::tranform with a call to std::for_each? My
documentation says that for_each must not modify the elements. Here is
modify_container using std::for_each rather than std::transform:

void modify_container(std::vector<BigType> &v)
{
std::for_each(v.begin(), v.end(), modify_element);
}

Any help is appreciated.
Jul 23 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a

"Jason Heyes" <ja********@optusnet.com.au> wrote in message
news:42**********************@news.optusnet.com.au ...
Look at my code:

void modify_element(BigType &x)
{
/* not shown */
}

BigType modify_element_copy(BigType x)
{
modify_element(x);
return x;
}

void modify_container(std::vector<BigType> &v)
{
std::transform(v.begin(), v.end(), v.begin(), modify_element_copy);
}

Can I replace the call to std::tranform with a call to std::for_each? My
documentation says that for_each must not modify the elements.
Here is
modify_container using std::for_each rather than std::transform:

void modify_container(std::vector<BigType> &v)
{
std::for_each(v.begin(), v.end(), modify_element);
}


Yes, 'for_each()' should work fine. Just remember not to try
using a modifying predicate with a const vector or via
const_iterators (well, the compiler should complain if you
do anyway).

-Mike
Jul 23 '05 #2

P: n/a
"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:Qe****************@newsread3.news.pas.earthli nk.net...

Yes, 'for_each()' should work fine. Just remember not to try
using a modifying predicate with a const vector or via
const_iterators (well, the compiler should complain if you
do anyway).

-Mike


Ok thanks for that.
Jul 23 '05 #3

P: n/a
The for_each algorithm does not modify the elements of the sequence. It
calls Function F for each element in the range [First, Last) and
returns the input parameter F. This function does not modify any
elements in the sequence.

Jul 23 '05 #4

P: n/a
The for_each algorithm does not modify the elements of the sequence. It

calls Function F for each element in the range [First, Last) and
returns the input parameter F. This function does not modify any
elements in the sequence. See "The Difference between for_each and
transform", C/C++ Users Journal, February 2001, Klaus Kreft &
Angelika Langer.

http://www.langer.camelot.de/Article...Transform.html

Jul 23 '05 #5

P: n/a
"Matthew Schaefer" <ma**************@yahoo.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
The for_each algorithm does not modify the elements of the sequence. It

calls Function F for each element in the range [First, Last) and
returns the input parameter F. This function does not modify any
elements in the sequence. See "The Difference between for_each and
transform", C/C++ Users Journal, February 2001, Klaus Kreft &
Angelika Langer.

http://www.langer.camelot.de/Article...Transform.html


I read the article and it contradicts what you just said. It states that
for_each "permits side effects including modification of elements from the
input sequence." You said "this function does not modify any elements in the
sequence." What is right?
Jul 23 '05 #6

P: n/a
In message <11**********************@o13g2000cwo.googlegroups .com>,
Matthew Schaefer <ma**************@yahoo.com> writes
The for_each algorithm does not modify the elements of the sequence. It
calls Function F for each element in the range [First, Last) and
returns the input parameter F. This function does not modify any
elements in the sequence.


The *algorithm* does not, of itself, modify any elements of the
sequence, and it discards the result of function F. That doesn't prevent
the function F from modifying its argument if you pass iterators of an
appropriate type.

--
Richard Herring
Jul 23 '05 #7

P: n/a
"Richard Herring" <ju**@[127.0.0.1]> wrote in message
news:vQ**************@baesystems.com...
In message <11**********************@o13g2000cwo.googlegroups .com>,
Matthew Schaefer <ma**************@yahoo.com> writes
The for_each algorithm does not modify the elements of the sequence. It
calls Function F for each element in the range [First, Last) and
returns the input parameter F. This function does not modify any
elements in the sequence.


The *algorithm* does not, of itself, modify any elements of the sequence,
and it discards the result of function F. That doesn't prevent the
function F from modifying its argument if you pass iterators of an
appropriate type.


The result of F eventually comes back as a return value, I believe.
Jul 23 '05 #8

P: n/a
In message <42**********************@news.optusnet.com.au>, Jason Heyes
<ja********@optusnet.com.au> writes
"Richard Herring" <ju**@[127.0.0.1]> wrote in message
news:vQ**************@baesystems.com...
In message <11**********************@o13g2000cwo.googlegroups .com>,
Matthew Schaefer <ma**************@yahoo.com> writes
The for_each algorithm does not modify the elements of the sequence. It
calls Function F for each element in the range [First, Last) and
returns the input parameter F. This function does not modify any
elements in the sequence.


The *algorithm* does not, of itself, modify any elements of the sequence,
and it discards the result of function F. That doesn't prevent the
function F from modifying its argument if you pass iterators of an
appropriate type.


The result of F eventually comes back as a return value, I believe.

No. A copy of F itself (the functor or function pointer) is returned:

template <class InputIterator, class Function>
Function for_each (InputIterator first, InputIterator last, Function f)
{
while (first != last) f(*first++);
return f;
}
--
Richard Herring
Jul 23 '05 #9

P: n/a
"Matthew Schaefer" <ma**************@yahoo.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
The for_each algorithm does not modify the elements of the sequence.
It calls the user supplied predicate function once for
each element, passing it a reference to the element.
The function can modify the referred to object if it likes.
It calls Function F for each element in the range [First, Last) and
returns the input parameter F.
This function does not modify any
elements in the sequence. See "The Difference between for_each and
transform", C/C++ Users Journal, February 2001, Klaus Kreft &
Angelika Langer.

http://www.langer.camelot.de/Article...ForEachTransfo
rm.html

Read it again. :-)

-Mike
Jul 23 '05 #10

P: n/a

"Richard Herring" <ju**@[127.0.0.1]> wrote in message
news:Cp**************@baesystems.com...
In message <42**********************@news.optusnet.com.au>, Jason Heyes
<ja********@optusnet.com.au> writes
"Richard Herring" <ju**@[127.0.0.1]> wrote in message
news:vQ**************@baesystems.com...
In message <11**********************@o13g2000cwo.googlegroups .com>,
Matthew Schaefer <ma**************@yahoo.com> writes
The for_each algorithm does not modify the elements of the sequence. It
calls Function F for each element in the range [First, Last) and
returns the input parameter F. This function does not modify any
elements in the sequence.

The *algorithm* does not, of itself, modify any elements of the sequence, and it discards the result of function F. That doesn't prevent the
function F from modifying its argument if you pass iterators of an
appropriate type.


The result of F eventually comes back as a return value, I believe.

No. A copy of F itself (the functor or function pointer) is returned:

template <class InputIterator, class Function>
Function for_each (InputIterator first, InputIterator last, Function f)
{
while (first != last) f(*first++);
return f;
}


Jason: Also note that if f() does return a value, it is
ignored. (So there's no point in f() having a return type
other than 'void', unless one plans to use it in other
contexts where the return value would be available).

-Mike

Jul 23 '05 #11

P: n/a
"Richard Herring" <ju**@[127.0.0.1]> wrote in message
news:Cp**************@baesystems.com...
In message <42**********************@news.optusnet.com.au>, Jason Heyes
<ja********@optusnet.com.au> writes
The result of F eventually comes back as a return value, I believe.

No. A copy of F itself (the functor or function pointer) is returned:

template <class InputIterator, class Function>
Function for_each (InputIterator first, InputIterator last, Function f)
{
while (first != last) f(*first++);
return f;
}


Oh yea. Quite right.
Jul 23 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.