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. 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
"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.
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 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
"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?
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
"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> writesThe 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.
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> writesThe 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
"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
"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
"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> writesThe 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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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;...
|
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...
|
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)
{...
|
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...
|
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...
|
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);...
|
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...
|
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...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
|
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...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was...
|
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...
|
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...
|
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...
|
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...
|
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....
|
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...
| |