473,757 Members | 10,007 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

About for_each usage

I'm having problems setting up the functors to use for_each. Here's an
example:

class A {
public:
void getName() { return name; }
private:
string name;
};

class B {
public:
vector<intgetId s { return vi; }
A& getA(int idx) { return vA.at(idx); }
void bfunc();
private:
vector<AvA;
vector<intvi;
}

class C {
public:
void cfunc();
private:
B b;
}

void B::bfunc()
{
// populate vi
for(int i=0; i<vi.size(); ++i) {
cout << vA[i].printName() << endl;
}
}

void C::cfunc()
{
// populate vi
vector<intvc = b.getIds();
for(int i=0; i<vc.size(); ++i) {
A& a = getA(vc[i]);
cout << a.getName() << endl;
}
}

Here vA is just a vector of As and vi is a vector of integers that
index into vA. Let's suppose that vA and vi are populated
appropriately. All I'm trying to do is to execute a particular
function on a subset of the As.

I have three questions:
1) How do I replace the for loop in bfunc with a for_each?
2) How do I replace the for loop in cfunc with a for_each?
3) How would I do this using the Boost.Lambda functions?

Thanks.
Jul 2 '08 #1
3 1517
On Jul 2, 3:27*pm, fgh.vbn....@gma il.com wrote:
I'm having problems setting up the functors to use for_each. Here's an
example:

class A {
public:
* * void getName() { return name; }
private:
* * string name;

};

class B {
public:
* * *vector<intgetI ds { return vi; }
* * *A& getA(int idx) { return vA.at(idx); }
* * *void bfunc();
private:
* * *vector<AvA;
* * *vector<intvi;

}

class C {
public:
* * *void cfunc();
private:
* * *B b;

}

void B::bfunc()
{
* *// populate vi
* *for(int i=0; i<vi.size(); ++i) {
* * * * cout << vA[i].printName() << endl;
* *}

}

void C::cfunc()
{
* *// populate vi
* *vector<intvc = b.getIds();
* *for(int i=0; i<vc.size(); ++i) {
* * * * A& a = getA(vc[i]);
* * * * cout << a.getName() << endl;
* *}

}

Here vA is just a vector of As and vi is a vector of integers that
index into vA. Let's suppose that vA and vi are populated
appropriately. All I'm trying to do is to execute a particular
function on a subset of the As.

I have three questions:
1) How do I replace the for loop in bfunc with a for_each?
2) How do I replace the for loop in cfunc with a for_each?
3) How would I do this using the Boost.Lambda functions?

Thanks.
Hi,

Your code has too many syntax errors as is to give advice on how to
modify it. Can you first make it compile and then repost it.

Ivan Novick
http://www.mycppquiz.com
Jul 2 '08 #2


Ivan Novick wrote:
On Jul 2, 3:27�pm, fgh.vbn....@gma il.com wrote:
I'm having problems setting up the functors to use for_each. Here's an
example:

class A {
public:
� � void getName() { return name; }
private:
� � string name;

};

class B {
public:
� � �vector<intge tIds { return vi; }
� � �A& getA(int idx) { return vA.at(idx); }
� � �void bfunc();
private:
� � �vector<AvA;
� � �vector<intvi ;

}

class C {
public:
� � �void cfunc();
private:
� � �B b;

}

void B::bfunc()
{
� �// populate vi
� �for(int i=0; i<vi.size(); ++i) {
� � � � cout << vA[i].printName() << endl;
� �}

}

void C::cfunc()
{
� �// populate vi
� �vector<intvc = b.getIds();
� �for(int i=0; i<vc.size(); ++i) {
� � � � A& a = getA(vc[i]);
� � � � cout << a.getName() << endl;
� �}

}

Here vA is just a vector of As and vi is a vector of integers that
index into vA. Let's suppose that vA and vi are populated
appropriately. All I'm trying to do is to execute a particular
function on a subset of the As.

I have three questions:
1) How do I replace the for loop in bfunc with a for_each?
2) How do I replace the for loop in cfunc with a for_each?
3) How would I do this using the Boost.Lambda functions?

Thanks.

Hi,

Your code has too many syntax errors as is to give advice on how to
modify it. Can you first make it compile and then repost it.

Ivan Novick
http://www.mycppquiz.com
Sorry, I should've tried it out first. Anyway, the following compiles
without errors on gcc v3.0.4.

#include <iostream>
#include <string>
#include <vector>

using namespace std;

class A {
public:
std::string getName() { return name; }
private:
std::string name;
};

class B {
public:
vector<intgetId s() { return vi; }
A& getA(int idx) { return vA.at(idx); }
void bfunc();
private:
vector<AvA;
vector<intvi;

};

class C {
public:
void cfunc();
private:
B b;
};

void B::bfunc()
{
// populate vA and vi in some fashion
for(int i=0; i<vi.size(); ++i) {
std::cout << vA[i].getName() << std::endl;
}
}

void C::cfunc()
{
// populate vA and vi in some fashion
vector<intvc = b.getIds();
for(int i=0; i<vc.size(); ++i) {
A& a = b.getA(vc[i]);
std::cout << a.getName() << std::endl;
}
}
Jul 3 '08 #3
fg*********@gma il.com wrote:
I'm having problems setting up the functors to use for_each. Here's an
example:

class A {
public:
void getName() { return name; }
error: return-statement with a value, in function returning 'void'
private:
string name;
};

class B {
public:
vector<intgetId s { return vi; }
error: invalid member function declaration
A& getA(int idx) { return vA.at(idx); }
void bfunc();
private:
vector<AvA;
vector<intvi;
}
error: missing semi-colon
class C {
public:
void cfunc();
private:
B b;
}
error: missing semi-colon
void B::bfunc()
{
// populate vi
for(int i=0; i<vi.size(); ++i) {
cout << vA[i].printName() << endl;
error: 'class A' has no member named 'printName'
}
}

void C::cfunc()
{
// populate vi
vector<intvc = b.getIds();
for(int i=0; i<vc.size(); ++i) {
A& a = getA(vc[i]);
error: 'getA' was not declared in this scope
cout << a.getName() << endl;
}
}

Here vA is just a vector of As and vi is a vector of integers that
index into vA.
Frankly, there a so many errors, that I don't think the loops do what
you want them to do, especially bfunc.
Let's suppose that vA and vi are populated
appropriately. All I'm trying to do is to execute a particular
function on a subset of the As.

I have three questions:
1) How do I replace the for loop in bfunc with a for_each?
Don't use for_each, use transform instead:

transform(vA.be gin(), vA.begin() + vi.size(),
ostream_iterato r<string>(cout , "\n"), mem_fun_ref(&A: :getName));

But again, I don't think that's what you want.
2) How do I replace the for loop in cfunc with a for_each?
That function really should be in class B. It pulls B's vi variable out,
and then iterates over B's vA variable. So let's put it there instead:

string B::getNameOf(in t x)
{
return vA.at(x).getNam e();
}

void B::cfunc()
{
transform(vi.be gin(), vi.end(), ostream_iterato r<string>(cout , "\n"),
bind1st(mem_fun (&B::getNameOf) , this));
}

3) How would I do this using the Boost.Lambda functions?
Differently.
Jul 3 '08 #4

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

Similar topics

5
4678
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().
3
1499
by: Griff | last post by:
#include <iostream> using namespace std; #include <vector> #include <string> #include <fstream> #include <algorithm> template<class C>void PrintAll(C&v) {
2
1520
by: flyaflya | last post by:
i use a class static member function to for_each: class t1 { static test(string s); } main() { list<string> slist; ...
1
1877
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:
12
2618
by: sashan | last post by:
Sometimes I write code using std::for_each. Sometimes I use it on several container classes that are the same size. for_each(v.begin(), v.end(), f1()); for_each(u.begin(), u.end(), f2()); This seems inefficient so I rewrite it as: int n = v.size(); for (int i = 0; i < n; ++i)
3
2886
by: PolkaHead | last post by:
I was wondering if there's a way to traverse a two-dimensional vector (vector of vectors) with a nested for_each call. The code included traverses the "outer" vector with a for_each, than it relies on the PrintFunctor to traverse the "inner" vector with another for_each. Is it possible to nest the second for_each, so I don't have to include a for_each in my function object. Is it possible to do a: for_each(myVec.begin(), myVec.end(),
3
1575
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...
4
1746
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>
9
4011
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) {}
0
9489
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
10072
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
9906
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
7286
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6562
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
5172
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
5329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3829
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
3
2698
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.