473,883 Members | 1,591 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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 3671
"Steven T. Hatton" <su******@setid ava.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******** ************@sp eakeasy.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_func tion<argument_t ype,
result_type>.

template < typename T > struct my_abs : std::unary_func tion<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.worldn et.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_func tion<argument_t ype,
result_type>.


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

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

The above has 2 subobjects of type std::binary_fun ction< 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_func tion< 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
4953
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 customIterator class I have defined the operators below (as well as others but they're not important right now, I don't think). class customIterator {
6
4632
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 obvious errors? (The function main is at the bottom of the file.) Code: http://www.grex.org/~kpp/cmultiknapsack.cpp To run this file you will need http://www.grex.org/~kpp/orlib1.txt in
30
1936
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
1308
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 to grasp at once that the function has global scope. I stretch this habit to using the operator on C library functions as well. So I write ::memset instead of memset and ::abs instead of abs. I vaguely remember hearing/ reading somewhere that it...
0
9943
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
11151
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10750
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10858
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10419
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7134
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5804
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5996
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4225
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.