By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,389 Members | 1,857 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,389 IT Pros & Developers. It's quick & easy.

STL Algorithm problem

P: 7
Hi, I'm having trouble with for_each.

Setup:

class test {

void f1(string txt) const {
for_each(txt.begin(),txt.end(),f2)
}
void f2(char a) const {
//do stuff with letters
}
}

This is the general outline of what i want to accomplish, but i can't get foreach to accept f2. I've tried mem_fun(f2), ptr_fun(f2). But I've got a feeling I'm way of. I'd appreciate any help i can get
Feb 20 '07 #1
Share this Question
Share on Google+
12 Replies


RedSon
Expert 5K+
P: 5,000
what is your for_each method? Why are you passing a function name to your other function?
Feb 20 '07 #2

P: 7
what is your for_each method? Why are you passing a function name to your other function?
for_each(begin, end, f) is a STL -algorithm, taking two iterators, and a funtion object. My problem is how to make it take the function f2 as an argument.

My problem could be solved with a loop
for(it=begin, it<end, it++)
f2(txt(it));

but for the sake of knowledge, I'd like to know how its done in STL
Feb 20 '07 #3

RedSon
Expert 5K+
P: 5,000
Did you write the for_each method? If so then can you post it here with some CODE tags around it so that I can take a look?
Feb 20 '07 #4

P: 7
Did you write the for_each method? If so then can you post it here with some CODE tags around it so that I can take a look?
Didn't write for_each. Comes with the stl-algorithm package
Feb 20 '07 #5

sicarie
Expert Mod 2.5K+
P: 4,677
Didn't write for_each. Comes with the stl-algorithm package
It looks like this link does what you are trying to do, though they don't post their implementation of f2. Does that help?
Feb 20 '07 #6

P: 7
No, thats different

their f2 is implemented in their example. Its a class inheriting from unary_function, implementing the () operator, and thus a functor. Also its global. What I have is an ordinary function inside a class, from wherein I use for_Each, wishing to use another ordinary function i the same class.
Feb 20 '07 #7

sicarie
Expert Mod 2.5K+
P: 4,677
No, thats different

their f2 is implemented in their example. Its a class inheriting from unary_function, implementing the () operator, and thus a functor. Also its global. What I have is an ordinary function inside a class, from wherein I use for_Each, wishing to use another ordinary function i the same class.
And can you change your definition to behave similarly to that? I'm thinking (and I dont' know how on-target I am, it's been about 2 years since I've done any large-scale c++ programming), is that you could overload f2 to behave like that (as you know an implementation that does work).
Feb 20 '07 #8

P: 7
As far as i know, no.

their f2 is a functor, otherwise known as a function object wich is a class behaving like a function.

What i have is a regular function. What really seems to mess things up, is that i have my implementation within a class. the following works perfectly

f2(char c) {
//do stuff
}

int main {
string txt="some string"
for_each(txt.begin,txt.end,f2)
}

But when i put the f2 in a class, calling it from another function in the same class, everything turns to shit

}
Feb 20 '07 #9

sicarie
Expert Mod 2.5K+
P: 4,677
As far as i know, no.

their f2 is a functor, otherwise known as a function object wich is a class behaving like a function.

What i have is a regular function. What really seems to mess things up, is that i have my implementation within a class. the following works perfectly

f2(char c) {
//do stuff
}

int main {
string txt="some string"
for_each(txt.begin,txt.end,f2)
}

But when i put the f2 in a class, calling it from another function in the same class, everything turns to shit

}
Hmmmm. Are you getting error messages, or is the output skewed?
Feb 20 '07 #10

P: 7
Error messages when compiling. The more i read about this the more convinced i get that what I'm trying to do can't be done. Atleast not the way I want it done
Feb 20 '07 #11

sicarie
Expert Mod 2.5K+
P: 4,677
Error messages when compiling. The more i read about this the more convinced i get that what I'm trying to do can't be done. Atleast not the way I want it done
Can you post the error message?
Feb 20 '07 #12

P: 7
Well, I get different error message depending on how i implement, and they all pretty much say that I'm supplying an unknown function, or that function blablabla doesn't match, candidates are blablabla. And this is for a function that i know should work (as i've tested it using my int main example). So simply supplying the name to the function doesn't work, and unless there is some special syntax to do this, using this method within a class isn't supported.

I've already changed the code, And I'm not to keen to put it back, so no error messages, sorry.
Feb 21 '07 #13

Post your reply

Sign in to post your reply or Sign up for a free account.