473,503 Members | 12,175 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

STL Algorithm problem

7 New Member
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
12 1296
RedSon
5,000 Recognized Expert Expert
what is your for_each method? Why are you passing a function name to your other function?
Feb 20 '07 #2
aimingformediocracy
7 New Member
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
5,000 Recognized Expert Expert
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
aimingformediocracy
7 New Member
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
4,677 Recognized Expert Moderator Specialist
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
aimingformediocracy
7 New Member
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
4,677 Recognized Expert Moderator Specialist
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
aimingformediocracy
7 New Member
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
4,677 Recognized Expert Moderator Specialist
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
aimingformediocracy
7 New Member
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
4,677 Recognized Expert Moderator Specialist
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
aimingformediocracy
7 New Member
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

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

Similar topics

6
8863
by: Jack Smith | last post by:
Hello, any help appreciated with following problem. I figured out the algorithm (I think), just having trouble proving it is optimal. Suppose we are given n tasks each of which takes 1 unit...
16
2636
by: cody | last post by:
I have to write an algorithm with must ensure that objects are put in buckets (which are always 4 in size). The objects have two properties: A and B. It is not allowed that in a bucket are objects...
17
6489
by: savesdeday | last post by:
In my beginnning computer science class we were asked to translate a simple interest problem. We are expected to write an algorithm that gets values for the starting account balance B, annual...
10
4955
by: bpontius | last post by:
The GES Algorithm A Surprisingly Simple Algorithm for Parallel Pattern Matching "Partially because the best algorithms presented in the literature are difficult to understand and to implement,...
113
12184
by: Bonj | last post by:
I was in need of an encryption algorithm to the following requirements: 1) Must be capable of encrypting strings to a byte array, and decyrpting back again to the same string 2) Must have the same...
4
4266
by: FBM | last post by:
Hi, I am working on a program that simulates one of the elements of ATM. The simulation stores events which occurs every some milliseconds for a certain amount of time. Every time that an event...
2
2130
by: Sherrie Laraurens | last post by:
Hi all, I'm trying to write a generic algorithm routine that will take begin and end iterators of a container, iterate through the range and perform a "calculation" of sorts. The trouble is...
2
7259
by: Julio C. Hernandez Castro | last post by:
Dear all, We have just developped a new block cipher called Raiden, following a Feistel Network structure by means of genetic programming. Our intention now consists on getting as much feedback...
10
2398
by: Sunway | last post by:
hello guys i have a bunsh of questions in java and algorithm most of them i didnt have a problem in them but two i had a diffculty in them could u help me in them please coz its urgent . the...
11
2262
by: Dijkstra | last post by:
Hi folks! First, this is the code I'm using to expose the problem: ------------------------------------------------------------------ #include <functional> #include <string> #include...
0
7067
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...
0
7264
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,...
0
7316
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...
1
4992
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...
0
4666
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...
0
3160
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...
0
3148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1495
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 ...
1
728
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.