| 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 +=
.
|
|
|
|
|

|
Related Articles |
|
|