I have a vector (v) containing objects of class C.
class C
{
private:
double d;
public:
void foo( B& b );
};
class B
{
public:
int i;
double x;
// lots of other things, which is why its passed by reference.
};
I'd like to run function foo for each element in v. foo takes one
argument of class B passed by reference. The following lines produces a
reference to reference error:
vector<Cv(50);
B b;
for_each( v.begin(), v.end(),
bind2nd( mem_fun_ref(&C::foo), b ) );
How can I get around this using for_each? 3 2355
Chris Roth wrote:
I have a vector (v) containing objects of class C.
class C
{
private:
double d;
public:
void foo( B& b );
};
class B
{
public:
int i;
double x;
// lots of other things, which is why its passed by reference.
};
I'd like to run function foo for each element in v. foo takes one
argument of class B passed by reference. The following lines produces a
reference to reference error:
vector<Cv(50);
B b;
for_each( v.begin(), v.end(),
bind2nd( mem_fun_ref(&C::foo), b ) );
How can I get around this using for_each?
Not with bind2nd and friends. You need boost::bind or tr1::bind. A
much more general and powerful solution anyway. You also get to forget
all about bind1st/2nd, mem_fun, mem_fun_ref, etc...
Without boost::bind and boost::ref you're going to be stuck making your
own functor or a for loop.
Noah Roberts wrote:
>
Not with bind2nd and friends. You need boost::bind or tr1::bind. A
much more general and powerful solution anyway. You also get to forget
all about bind1st/2nd, mem_fun, mem_fun_ref, etc...
Without boost::bind and boost::ref you're going to be stuck making your
own functor or a for loop.
Yep, I gave up and went boost with this also:
#include <vector>
#include <algorithm>
#include <boost/bind.hpp>
using namespace std;
using namespace boost;
class B
{
public:
int i;
double x;
};
class C
{
private:
double d;
public:
void foo( B& b ){}
};
int
main()
{
vector<Cv(50);
B b;
//for_each( v.begin(), v.end(), mem_fun_ref(&C::foo), b ) );
for_each( v.begin(), v.end(),
bind(&C::foo, _1, ref(b)));
}
On Mar 9, 6:09 pm, Chris Roth <czr...@mail.usask.cawrote:
I have a vector (v) containing objects of class C.
class C
{
private:
double d;
public:
void foo( B& b );
};
class B
{
public:
int i;
double x;
// lots of other things, which is why its passed by reference.
};
I'd like to run function foo for each element in v. foo takes one
argument of class B passed by reference. The following lines produces a
reference to reference error:
vector<Cv(50);
B b;
for_each( v.begin(), v.end(),
bind2nd( mem_fun_ref(&C::foo), b ) );
How can I get around this using for_each?
class MyFunctor
{
public:
MyFunctor (B & b) : m_b (b)
{}
void operator () (C & c)
{
c.foo (m_b);
}
private:
B & m_b;
}
std::for_each (v.begin (), v.end (), MyFunctor (b)); This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: John Black |
last post by:
Hi,
I am not familiar with for_each very well, suppoase I have a
vector<pair<unsigned int, unsigned int> > vec1 and the contents are
{<0x00000000, 0x000000FF>, <0x10000000, 0x2FFFFFFF>}
what...
|
by: trash |
last post by:
I do this kindda stuff often:
class CVisitableObjectVector : public
std::vector<Loki::BaseVisitable<>*> {
void Enable(bool enable);
....
};
|
by: franklini |
last post by:
hello people,
just wanted to say thanks again for the help in the past. i have a new
problem which am wondering if any body can help me with.
i have written this abtract class shape and its...
|
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...
|
by: ibe |
last post by:
Hi,
i can't get the clue:
I hav a loop in my code like this:
---snip---
class Foo {
accept(Visitor& v) {
v.visit(*this);
}
|
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...
|
by: benben |
last post by:
I wrote the following lines:
#include <locale>
#include <functional>
#include <algorithm>
int main()
{
using namespace std;
bind2nd(ptr_fun(tolower<char>), locale());
|
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) :...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
| |