Hi Everyone
So, I want to use a std::for_each in the following situation:
class B{
//whatever
}
class A{
private:
void configure_A_fro m_B(const std::vector<B*> & bPtrVec);
void member_function (B*); //non const.
}
A::configure_A_ from_B(const std::vector<B*> & bPtrVec){
std::for_each( bPtrVec.begin() , bPtrVec.end(), member_function () )
}
i.e. I want to use std::for_each to call a member (of A) function,
with a single parameter, the pointer pointed to by the iterator I am
for_eaching over.
Unfortunatley I'm tripped up by the whole binding thing... can someone help
me out? I'm also willing to use the boost lambda libraries, but I haven't
been
able to sort this out.
Thanks,
Glen 5 2692
one way to use the for_each you need to create a function object. This
can be done by overriding the operator() of the class.
delare the class A as
class A{
private:
void configure_A_fro m_B(const std::vector<B*> & bPtrVec);
//void member_function (B*); //non const.
void operator()(cons t B* value);
}
A::operator()(c onst B* value)
{
//do what ever you want to do with B*
}
The function A::configure_A_ from_B will be modified as :
A::configure_A_ from_B(const std::vector<B*> & bPtrVec){
std::for_each( bPtrVec.begin() , bPtrVec.end(), A());
}
"glen stark" <st***@ife.ee.e thz.ch> wrote in message
news:43******** @news1.ethz.ch. .. Hi Everyone
i.e. I want to use std::for_each to call a member (of A) function, with a single parameter, the pointer pointed to by the iterator I am for_eaching over.
Unfortunatley I'm tripped up by the whole binding thing... can someone help me out? I'm also willing to use the boost lambda libraries, but I haven't been able to sort this out.
Use the boost bind library: So, I want to use a std::for_each in the following situation:
#include <boost/bind.hpp>
class B{ //whatever }
class A{ private: void configure_A_fro m_B(const std::vector<B*> & bPtrVec); void member_function (B*); //non const. }
A::configure_A_ from_B(const std::vector<B*> & bPtrVec){ std::for_each( bPtrVec.begin() , bPtrVec.end(), member_function () )
std::for_each( bPtrVec.begin()
, bPtrVec.end()
, boost::bind( &A::member_func tion, this, _1 )
);
}
Jeff Flinn
<ra************ @gmail.com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.com.. . one way to use the for_each you need to create a function object. This can be done by overriding the operator() of the class.
Thanks for the suggestion.I forgot to mention, I don't want to create an
explicit function object. I run into this situation all the time, and it
seems to me that there must be a way to bind the member function (perhaps
using boost lambda libraries
to be able to do this in a quick clean way without having to create a
function object for every instance I want to do this.
glen stark wrote: Hi Everyone
So, I want to use a std::for_each in the following situation:
class B{ //whatever }
class A{ private: void configure_A_fro m_B(const std::vector<B*> & bPtrVec); void member_function (B*); //non const. }
A::configure_A_ from_B(const std::vector<B*> & bPtrVec){ std::for_each( bPtrVec.begin() , bPtrVec.end(), member_function () ) }
i.e. I want to use std::for_each to call a member (of A) function, with a single parameter, the pointer pointed to by the iterator I am for_eaching over.
Unfortunatley I'm tripped up by the whole binding thing... can someone help me out? I'm also willing to use the boost lambda libraries, but I haven't been able to sort this out.
Do you mean boost::bind?
std::bind1st and std::mem_fun combination do not allow a non-const
member function so you'd have to try either boost::bind or write your
own function object.
boost::bind would work (I think) thus:
std::for_each( bPtrVec.begin() , bPtrVec.end(),
boost::bind( &A::member_func tion, this, _1 ) );
They're not very clear with their examples for this typical situation
(I don't know why).
> Use the boost bind library: So, I want to use a std::for_each in the following situation:
#include <boost/bind.hpp>
class B{ //whatever }
class A{ private: void configure_A_fro m_B(const std::vector<B*> & bPtrVec); void member_function (B*); //non const. }
A::configure_A_ from_B(const std::vector<B*> & bPtrVec){ std::for_each( bPtrVec.begin() , bPtrVec.end(), member_function () )
std::for_each( bPtrVec.begin() , bPtrVec.end() , boost::bind( &A::member_func tion, this, _1 ) );
}
Jeff Flinn
Thanks, that's just what I was looking for. I tried the bind library, but
didn't get the syntax right for
the member function (didn't have the 'this' argument).
glen This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Alex Vinokur |
last post by:
Functor-parameters in the for_each() and transform() algorithms are passed by value.
Might it make sense to have also algorithms for_each2() and transform2()
which pass Functor-parameters by non-const reference?
Here is some program which demonstrates probable necessity
in such forms of for_each() and transform().
|
by: Capstar |
last post by:
Hi NG,
I have a question on std::for_each. I try to use this in combination
with std::bind2nd to call a method of all functions in a container
(std::set or std::map) and pass that method the second argument of
std::bind2nd.
But for some reason the compiler wants met ot have all const objects,
which obviously doesn't work when the method is non-const.
example:
|
by: waitan |
last post by:
#include <algorithm>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <iterator>
#include <iomanip>
using namespace std;
|
by: Chris Roth |
last post by:
I have a vector of vectors:
vector< vector<double v;
and have initialized it with:
v( 5 );
as I know I will have 5 columns of data. At this point, I read text file
data into each of the the vectors using push_back. I know that I will be
reading in 5000 elements into each vector, so I use reserve:
ifstream f( "file.txt" );
if(f.is_open())
{
|
by: Eric Lilja |
last post by:
Hello, in one of my classes I have this member variable:
Tile ***tiles_; where Tile is another one of my classes.
dimensional array of Tile-pointers (dimensions are of equal sizes).
Last, I allocate a Tile object for each pointer in the 2d-array.
Anyway, I often need to traverse the entire array and perform
operations so until now I've used alot of constructs like this:
for (int y = 0; y < num_rows; ++y)
{
| |
by: Yan |
last post by:
Hi,
I don't seem to be able to use for_each if it should replace a 'for'
loop in a method (constructor in my case) and inside that 'for' loop a
class member variable is being accessed. The presence of this member
variable prevents me from using a static or global method to be passed
as a third parameter to for_each, and mem_fun doesn't seem to work for
me either as I am not going to execute a method of an iterator but
pass an iterator...
|
by: Kenneth Porter |
last post by:
I'm trying to create a class that represents a bucket of locks, and I
want to lock a set of objects specified by a container of pointers. I
can't seem to find the right combination of function adapters to invoke
the bucket's member function on each pointer in the argument container.
What's the right thing to use here, at the "???"?
#include <vector>
#include <functional>
#include <algorithm>
|
by: nguillot |
last post by:
Hello
I used to loop on a std::map<k, dto act on the data (d) like that (d
being a class with setIntMember method):
typedef std::map<k, dtMap;
struct setIntMember
{
setIntMember(int j) : i(j) {}
|
by: Ruben |
last post by:
error: passing `const Weight' as `this' argument of `float
Weight::wgt()' discards qualifiers
seems to be some sort of standard error format that I'm not understanding.
I have code that looks like this
header file
|
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...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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...
|
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...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |