STL algorithm for implementing operator+=

J

Jef Driesen

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

Ulrich Achleitner

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?
 
A

alexmdac

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" ) );
}
 
J

Jef Driesen

Ulrich said:
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 +=
 
R

red floyd

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top