STL algorithm for implementing operator+=



 DEVELOP > c-Plus-Plus > STL algorithm for implementing operator+=

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Jef Driesen"
Date: 26 Jan 2005 04:28:25 AM
Object: 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.
.

User: ""

Title: Re: STL algorithm for implementing operator+= 26 Jan 2005 05:51:18 AM
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" ) );
}
.
User: "red floyd"

Title: for_each() and modifying sequence contents. 26 Jan 2005 11:19:27 AM
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?
.


User: "Ulrich Achleitner"

Title: Re: STL algorithm for implementing operator+= 26 Jan 2005 05:17:06 AM
On Wed, 26 Jan 2005 11:28:25 +0100, Jef Driesen
<jefdriesen@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
.
User: "Jef Driesen"

Title: Re: STL algorithm for implementing operator+= 26 Jan 2005 07:09:20 AM
Ulrich Achleitner wrote:

On Wed, 26 Jan 2005 11:28:25 +0100, Jef Driesen
<jefdriesen@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 +=
.



  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER