473,735 Members | 2,326 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_containe r(std::vector<B igType> &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_containe r using std::for_each rather than std::transform:

void modify_containe r(std::vector<B igType> &v)
{
std::for_each(v .begin(), v.end(), modify_element) ;
}

Any help is appreciated.
Jul 23 '05 #1
11 3927

"Jason Heyes" <ja********@opt usnet.com.au> wrote in message
news:42******** **************@ news.optusnet.c om.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_containe r(std::vector<B igType> &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_containe r using std::for_each rather than std::transform:

void modify_containe r(std::vector<B igType> &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******@mkwah ler.net> wrote in message
news:Qe******** ********@newsre ad3.news.pas.ea rthlink.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******** *************@f 14g2000cwb.goog legroups.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************ **********@o13g 2000cwo.googleg roups.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******** ******@baesyste ms.com...
In message <11************ **********@o13g 2000cwo.googleg roups.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.a u>, Jason Heyes
<ja********@opt usnet.com.au> writes
"Richard Herring" <ju**@[127.0.0.1]> wrote in message
news:vQ******* *******@baesyst ems.com...
In message <11************ **********@o13g 2000cwo.googleg roups.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******** *************@f 14g2000cwb.goog legroups.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

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

Similar topics

27
5967
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.
20
17829
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; std::vector<double>::size_type size = src.size(); dest.reserve(size); for (std::vector<int>::size_type i = 0;
17
3355
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 this uovec at the moment (for Unit-Offset VECtor). I want the class to respond correctly to all usage of STL containers and algorithms so that it is a transparent replacement for std:vector. The options seems to be:
8
4911
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 std::vector::erase, it calls T::operator= only three times no matter
32
69688
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: std::vector<int> fun(){ //build the vector v; return v; }
56
5796
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
6
2012
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;
5
12137
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++: for each element of the vector for each subsequent element of the vector if the belong together <some code to compare them> then merge the elements (add the 2nd to the 1st, then delete the 1st)
13
2959
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 {
0
8958
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
9460
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
9323
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
9248
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
9193
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
6747
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
6045
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4821
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2737
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.