473,404 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

STL algorithm for implementing operator+=

Is there an STL algorithm that does something like

for (iterator i = begin(); i != end(); ++i)
f(*i,value);

where f results in the expression *i += value. I already found the
algorithm 'transform' with the 'plus' function object, but it does
evaluate to *i = *i + value. And this is not exactly what I want.
Jul 23 '05 #1
4 1773
On Wed, 26 Jan 2005 11:28:25 +0100, Jef Driesen
<je********@hotmail.com.nospam> wrote:
Is there an STL algorithm that does something like

for (iterator i = begin(); i != end(); ++i)
f(*i,value);

where f results in the expression *i += value. I already found the
algorithm 'transform' with the 'plus' function object, but it does
evaluate to *i = *i + value. And this is not exactly what I want.

maybe

template<class InputIterator, class Distance> void advance(InputIterator&
_InIt, Distance _Off);

from <iterator> is what you need?

--
have a nice day
ulrich
Jul 23 '05 #2
Here's how to make your own functor to do what you want.

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

template< typename T, typename A >
class Incrementor {
public:
Incrementor( const A &amount ) : m_Amount(amount)
{}

void operator()( T &t )
{ t += m_Amount; }

private:
A m_Amount;
};

class Test {
public:
template< typename T >
Test & operator += ( const T &t )
{ cout << "operator += ( " << t << " )" << endl; }
};

int main()
{
vector< Test > tests(5);

for_each( tests.begin(), tests.end(),
Incrementor< Test, int >(3) );

for_each( tests.begin(), tests.end(),
Incrementor< Test, const char * >( "hello" ) );
}

Jul 23 '05 #3
Ulrich Achleitner wrote:
On Wed, 26 Jan 2005 11:28:25 +0100, Jef Driesen
<je********@hotmail.com.nospam> wrote:
Is there an STL algorithm that does something like

for (iterator i = begin(); i != end(); ++i)
f(*i,value);

where f results in the expression *i += value. I already found the
algorithm 'transform' with the 'plus' function object, but it does
evaluate to *i = *i + value. And this is not exactly what I want.


maybe

template<class InputIterator, class Distance> void
advance(InputIterator& _InIt, Distance _Off);

from <iterator> is what you need?


I don't need to advance the iterator itself, but add (or substract,
multiply, divide,...) a value to the object where the iterator points
to. I want this to implement these operators for my own container (2D
vector):

vector<T>& operator+=(const vector<T>& rhs)
vector<T>& operator+=(const T& rhs)

without having to rewrite the same loop in every function. The problem
is not the algorithm (I can write my own) but the function object for
operator+= on the elements. In the copy constructor and assignment
operator I can use std::copy and std::fill. I want something similar for
operator +=
Jul 23 '05 #4
al******@hotmail.com wrote:
Here's how to make your own functor to do what you want.

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

template< typename T, typename A >
class Incrementor {
public:
Incrementor( const A &amount ) : m_Amount(amount)
{}

void operator()( T &t )
{ t += m_Amount; }

private:
A m_Amount;
};

class Test {
public:
template< typename T >
Test & operator += ( const T &t )
{ cout << "operator += ( " << t << " )" << endl; }
};

int main()
{
vector< Test > tests(5);

for_each( tests.begin(), tests.end(),
Incrementor< Test, int >(3) );

for_each( tests.begin(), tests.end(),
Incrementor< Test, const char * >( "hello" ) );
}


Didn't we go through this a couple of months ago? With the definition
of mutating vs. non-mutating for for_each()?

I don't remember the resolution. I know that for_each() leaves the
sequence unchanged, but is it allowed to modify elements of the sequence
in place?

Jul 23 '05 #5

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

Similar topics

2
by: Marcin Kielar | last post by:
hello i'm searching for algorithm able to detect and resolve conflicts during recursive event propagation. I'd like to implement small message passing framework without using message queue -...
13
by: M | last post by:
Hi, I've searched through the previous posts and there seems to be a few examples of search and replacing all occurrances of a string with another string. I would have thought that the code...
3
by: Asbjørn Ulsberg | last post by:
I'm writing a little C# / DirectSound piano which uses SecondaryBuffer to play .wav-files. The SecondaryBuffer has a Frequenzy property which I set to modulate the .wav file into playing the right...
7
by: Edward Diener | last post by:
Since implement the assign operator for reference types eliminates the ability to assign a reference object to a reference variable of the same type or base class of that type, I assume that...
2
by: Sherrie Laraurens | last post by:
Hi all, I'm trying to write a generic algorithm routine that will take begin and end iterators of a container, iterate through the range and perform a "calculation" of sorts. The trouble is...
10
by: vermarajeev | last post by:
Hi guys, I have problem regarding find algorithm used in STL Defination directly from book The find algorithm is an operation (function) that can be applied to many STL containers. It...
13
by: Tristan Wibberley | last post by:
Hi I've got implementing overloaded operator new and delete pretty much down. Just got to meet the alignment requirements of the class on which the operator is overloaded. But how does one...
10
by: arnuld | last post by:
WANTED: /* C++ Primer - 4/e * * Exercise: 9.26 * STATEMENT * Using the following definition of ia, copy ia into a vector and into a list. Use the single iterator form of erase to...
20
by: mike3 | last post by:
Hi. (Xposted to both comp.lang.c++ and comp.programming since I've got questions related to both C++ language and general programming) I've got the following C++ code. The first routine runs in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
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...
0
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...
0
agi2029
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,...

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.