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 3887
"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
counted implementation of std::vector? Thanks.
|
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;
std::vector<int> dest;
...
|
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 returns
positions of elements etc. I am calling...
|
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)
{
std::swap(v, v.back());
v.resize(index);
}
Unlike...
|
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 function is called.
I can do like this:
...
|
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 syntax right.
Can anyone please help? Thanks
|
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);
x.push_back(-2);
x.push_back(-7);
int i = 3;
|
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 something like the following pseudocode in C++:
...
|
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 <boost/tuple/tuple.hpp>
class {
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
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,...
|
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...
|
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,...
| |