472,353 Members | 2,006 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

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

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
11 3775

"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
"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
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
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
"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
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
"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
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
"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

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

Similar topics

27
by: Jason Heyes | last post by:
To my understanding, std::vector does not use reference counting to avoid the overhead of copying and initialisation. Where can I get a reference...
20
by: Anonymous | last post by:
Is there a non-brute force method of doing this? transform() looked likely but had no predefined function object. std::vector<double> src;...
17
by: Michael Hopkins | last post by:
Hi all I want to create a std::vector that goes from 1 to n instead of 0 to n-1. The only change this will have is in loops and when the vector...
8
by: Jason Heyes | last post by:
Does the STL have a function like this one? template <typename T> void remove(std::vector<T> &v, std::vector<T>::size_type index) {...
32
by: zl2k | last post by:
hi, c++ user Suppose I constructed a large array and put it in the std::vector in a function and now I want to return it back to where the...
56
by: Peter Olcott | last post by:
I am trying to refer to the same std::vector in a class by two different names, I tried a union, and I tried a reference, I can't seem to get the...
6
by: imutate | last post by:
How to directly reference i th element in std::vector (i being an integer) ? Example: std::vector<doublex; x.push_back(3); x.push_back(-2);...
5
by: Alan | last post by:
I was wondering whether it is good programming practice or asking for trouble to modify a vector while iterating through it. That is, I want to do...
13
by: jubelbrus | last post by:
Hi I'm trying to do the following. #include <vector> #include <boost/thread/mutex.hpp> #include <boost/shared_ptr.hpp> #include...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

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.