473,396 Members | 1,966 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,396 software developers and data experts.

std::transform container => std::abs(container)

This code works for dividing each element of a boost::array<> by a value of
its element type:

template <typename T, size_t S>
inline boost::array<T, S>& operator/=( boost::array<T, S>& lhs, const T&
rhs ) {
std::transform( lhs.begin()
, lhs.end()
, lhs.begin()
, std::bind2nd( std::divides<T>(), rhs ) );
return lhs;
}

I want to use std::transform in a similar fashion to set each element of a
boost::array<> to the result of applying std::abs() to it.

The result can easily be obtained with this code:

template <typename T, size_t Order_S>
inline boost::array<T, Order_S>& abs( boost::array<T, Order_S>& v ) {
for ( size_t i = 0; i < v.size(); i++ ) {
v[ i ] = std::abs( v[ i ] );
}
return v;
}

I would like to know how to apply a function such as std::abs in a way
similar to the use of std::bind2nd ( std::divides<T>(), rhs ) in the above
example. How can this be done?

--
"If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true." - Bertrand
Russell

Jul 22 '05 #1
4 3640
"Steven T. Hatton" <su******@setidava.kushan.aa> wrote in message
std::transform( lhs.begin()
, lhs.end()
, lhs.begin()
, std::bind2nd( std::divides<T>(), rhs ) ); I want to use std::transform in a similar fashion to set each element of a
boost::array<> to the result of applying std::abs() to it.


Here is one way

int (*absolute)(int) = &std::abs;
std::transform(lhs.begin()
, lhs.end()
, lhs.begin()
, absolute );

Jul 22 '05 #2
Steven T. Hatton wrote in news:AK********************@speakeasy.net in
comp.lang.c++:
This code works for dividing each element of a boost::array<> by a
value of its element type:

template <typename T, size_t S>
inline boost::array<T, S>& operator/=( boost::array<T, S>& lhs,
const T&
rhs ) {
std::transform( lhs.begin()
, lhs.end()
, lhs.begin()
, std::bind2nd( std::divides<T>(), rhs ) );
return lhs;
}

I want to use std::transform in a similar fashion to set each element
of a boost::array<> to the result of applying std::abs() to it.

The result can easily be obtained with this code:

template <typename T, size_t Order_S>
inline boost::array<T, Order_S>& abs( boost::array<T, Order_S>& v
) {
for ( size_t i = 0; i < v.size(); i++ ) {
v[ i ] = std::abs( v[ i ] );
}
return v;
}

I would like to know how to apply a function such as std::abs in a way
similar to the use of std::bind2nd ( std::divides<T>(), rhs ) in the
above example. How can this be done?


Write you're own functor:

template < typename T > struct my_abs
{
typedef T return_type;

T operator () ( T const &arg ) const
{
return std::abs( arg );

/* or even better (ADL friendly version):
*/
using std::abs;
return abs( arg );
}
};

The other alternative is to put a cast into the call to
std::transform:

std::transform(
lhs.begin(), lhs.end(), lhs.begin(),
static_cast< T (*)(T) >( std::abs )
);
The disadvantage here is that for T = short (for example) there
is no overload short std::abs( short ), so the static_cast can't
succeed. Also Argument Dependant Lookup (ADL) can't be used.

Remember to include <cstdlib> and <cmath> so that you get all the
overloads of std::abs, also unless you use an ADL friendly functor
you'll need to include <complex> before calling std::abs if you want
your code to work with std::complex (this also applies valarray).

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #3
"Rob Williscroft" <rt*@freenet.co.uk> wrote in message
template < typename T > struct my_abs
{
typedef T return_type;

T operator () ( T const &arg ) const
{
return std::abs( arg );

/* or even better (ADL friendly version):
*/
using std::abs;
return abs( arg );
}
};


The standard typedef names are result_type and argument_type. These allow
compatibility with other standard binders in STL and boost like
std::bind2nd.

template < typename T > struct my_abs
{
typedef T argument_type;
typedef T result_type;
T operator () ( T const &arg ) const
{
using std::abs;
return abs( arg );
}
};

My preferred way is to derive from std::unary_function<argument_type,
result_type>.

template < typename T > struct my_abs : std::unary_function<T, T>
{
T operator () ( T const &arg ) const
{
using std::abs;
return abs( arg );
}
};

Jul 22 '05 #4
Siemel Naran wrote in
news:u9**********************@bgtnsc04-news.ops.worldnet.att.net in
comp.lang.c++:

The standard typedef names are result_type
Thanks I always get this wrong.
and argument_type.
AIUI (I haven't researched this myself, just going on snippets
from usenet) argument_type isn't needed (used).
These
allow compatibility with other standard binders in STL and boost like
std::bind2nd.

template < typename T > struct my_abs
{
typedef T argument_type;
typedef T result_type;
T operator () ( T const &arg ) const
{
using std::abs;
return abs( arg );
}
};

My preferred way is to derive from std::unary_function<argument_type,
result_type>.


Yep, but that does add an extra byte to the function object in some
cases:

struct my_functor : std::binary_function< int, int, bool >
{
std::less< int > m_less;
// operator here ...
};

The above has 2 subobjects of type std::binary_function< int, int, bool >
so EBO (Empty Base (class) Optimization) can't happen.

I am perhapse being needlessly pedantic (and possibly I'm premeturly
optimizing), OTOH typing "typedef T result_type;" is shorter (and IMO
less cryptic) than writing " : std::unary_function< T, T > ".

If only I could remember result_type not return_type all would be
well :).

Either way I have to lookup result_type or binary_function /
unary_function as I'm actualy not sure the bool return type above
shouldn't be the first argument.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #5

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

Similar topics

13
by: Grahamo | last post by:
Hi, I'm implementing a custom iterator (random access type ) so I can use stl algorithms such as sort on my legacy containers. I'm having problems compiling however. when implementing my...
6
by: KS | last post by:
Hello, I'm writing some c++ code after a few years with Java and your help is appreciated. I am getting a core dump on a call to qsort. Can you take a look at the code and see if there are any...
30
by: Filimon Roukoutakis | last post by:
Suppose that we have a function f(Object*& obj) and have declared a global std::vector<Object*vec; Is it valid to do void g() { vec.push_back(new Object);
6
by: phdscholar80 | last post by:
I have the habit of using the scope resolution operator whenever I use global functions. My premise is that it improves readability by helping someone who is looking at the code for the first time...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
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,...
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...

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.