473,903 Members | 4,843 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
11 3939

"Richard Herring" <ju**@[]> wrote in message
news:Cp******** ******@baesyste ms.com...
In message <42************ **********@news .optusnet.com.a u>, Jason Heyes
<ja********@opt usnet.com.au> writes
"Richard Herring" <ju**@[]> 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;

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).


Jul 23 '05 #11
"Richard Herring" <ju**@[]> wrote in message
news:Cp******** ******@baesyste ms.com...
In message <42************ **********@news .optusnet.com.a u>, Jason Heyes
<ja********@opt usnet.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

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; std::vector<double>::size_type size = src.size(); dest.reserve(size); for (std::vector<int>::size_type i = 0;
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:
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
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; }
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++: 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)
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: 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...
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...
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...
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...
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
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...
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();...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
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.