473,322 Members | 1,644 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

help in solving this function object.

I am trying to learn STL but got stuck in for_each().
What I intend to do in the following is to make a list with each
element as a string.
add the string elements to the list until it has 10 elements. each
element is the same 1234567890. then I want to use for_each to iterate
the list, and for each to call apply() whihc is create a filen with
name the same as string al; and within each file, it contains the
content of the list element.
I go stuck in making apply to work. Can anyone help?? the following is
the code, it compiles under vc++ 8.
Thanks
#include <algorithm>
#include <list>
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>

using namespace std;

class makefile{

public:
void operator()(std::string sth, std::string sth2){

};
void apply(std::string s, std::string name){
std::filebuf buffer;
std::ostream output(&buffer);
buffer.open(name.c_str(), std::ios::out);
output << s << std::endl;
}
};

int main(){
std::string s("1234567890");
std::string al("abc");
list<std::stringlString;
for (int i=0; i <= 9; i++) {
//std::string tmp(itoa(i));
al = s+ s.at(i);
lString.push_back(s);
}
//for_each();

for_each(lString.begin(),lString.end(),apply());
return 0;
}

Aug 29 '06 #1
6 1666

Code needs a lot of help, but the biggest problems are;

1) for_each expects a function object that takes one param, while
makefile::operator() expects 2 params. No matching func...

2) Your definition of makefile::operator() does not call
makefile::apply() at all. operator() is empty. You seem to
misunderstand
function objects. In your code, makefile::apply() never gets called.

3) you don't have an instance of makefile class. You need to
instantiate and
get an object. Something like;

makefile my_obj;
for_each(lString.begin(), lString.end(), my_obj);

(Note that it's my_obj, not my_obj() above...)

for_each will call my_obj() which would execute operator() in your
makefile class.

Hope this helps,

Tolga Ceylan

Aug 29 '06 #2

to***********@yahoo.com wrote:
Code needs a lot of help, but the biggest problems are;

1) for_each expects a function object that takes one param, while
makefile::operator() expects 2 params. No matching func...

2) Your definition of makefile::operator() does not call
makefile::apply() at all. operator() is empty. You seem to
misunderstand
function objects. In your code, makefile::apply() never gets called.

3) you don't have an instance of makefile class. You need to
instantiate and
get an object. Something like;

makefile my_obj;
for_each(lString.begin(), lString.end(), my_obj);

(Note that it's my_obj, not my_obj() above...)

for_each will call my_obj() which would execute operator() in your
makefile class.

Hope this helps,

Tolga Ceylan
Thanks Tolga.
So what should I do if the operation that I apply to each element of
the list needs 2 input parameters?
If I change the makefile object to take 1 parameter -- std:string for
each element in a list, but I also need the client have a say to change
the filename of the output file. Can I archieve this using for_each? I
can make the whoe thing with a for-loop, but I am trying to see if
for_each or other tools inside algorithm can do the trick for me.
Thanks

Aug 29 '06 #3
learning schrieb:
I am trying to learn STL but got stuck in for_each().
What I intend to do in the following is to make a list with each
element as a string.
add the string elements to the list until it has 10 elements. each
element is the same 1234567890. then I want to use for_each to iterate
the list, and for each to call apply() whihc is create a filen with
name the same as string al; and within each file, it contains the
content of the list element.
I go stuck in making apply to work. Can anyone help?? the following is
the code, it compiles under vc++ 8.
It doesn't compile.
Thanks
#include <algorithm>
#include <list>
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>

using namespace std;

class makefile{

public:
void operator()(std::string sth, std::string sth2){

};
void apply(std::string s, std::string name){
Put this code in operator().

for_each supplies only one argument, so it should be:

void operator()(std::string name) // or:
void operator()(const std::string& name)
std::filebuf buffer;
std::ostream output(&buffer);
buffer.open(name.c_str(), std::ios::out);
output << s << std::endl;
Why don't you use a simple ofstream object?

std::ofstream output(name.c_str());
output << s << std::endl;
}
};

int main(){
std::string s("1234567890");
std::string al("abc");
list<std::stringlString;
for (int i=0; i <= 9; i++) {
//std::string tmp(itoa(i));
al = s+ s.at(i);
What is 'al' for?
lString.push_back(s);
}
//for_each();

for_each(lString.begin(),lString.end(),apply());
for_each(lString.begin(), lString.end(), makefile());
return 0;
}
Your makefile() functor opens a file with name given to it and outputs a
string given as second argument. for_each can only work with one element at
a time.
If you want multiple files with different output strings, try a
std::list< std::pair<std::string, std::string
with first string being the filename and second string the text to output.

The functor would be like this:

struct makefile
{
void operator()(const std::pair<std::string, std::string>& record) const
{
std::ofstream file(record.first.c_str());
file << record.second << std::endl;
}
}

--
Thomas
Aug 29 '06 #4
In article <11**********************@p79g2000cwp.googlegroups .com>,
"learning" <ed**********@gmail.comwrote:
I am trying to learn STL but got stuck in for_each().
What I intend to do in the following is to make a list with each
element as a string.
add the string elements to the list until it has 10 elements. each
element is the same 1234567890. then I want to use for_each to iterate
the list, and for each to call apply() whihc is create a filen with
name the same as string al; and within each file, it contains the
content of the list element.
I go stuck in making apply to work. Can anyone help?? the following is
the code, it compiles under vc++ 8.
Really? It compiles? If so then you need to throw away your compiler.

struct makefile
{
void operator()( const string& s ) const {
// here you want to "create a filen with name the same as string
// al; and within each file, it contains the content of the list
// element."
// are you sure you want to create the same file over and over
// again though?
}
};

int main()
{
list< string strings;
// fill 'strings'
for_each( strings.begin(), strings.end(), makefile() );
}
Aug 29 '06 #5
"learning" <ed**********@gmail.comwrote:
>
So what should I do if the operation that I apply to each element
of the list needs 2 input parameters?
Where are those parameters coming from?
If I change the makefile object to take 1 parameter -- std:string
for each element in a list, but I also need the client have a say
to change the filename of the output file. Can I archieve this
using for_each?
Do you want each element to have a different value for the second
parameter or the same value? Where is the value or values coming from?
I can make the whoe thing with a for-loop, but I am trying to see
if for_each or other tools inside algorithm can do the trick for me.
Show us a for loop so we can see what you are trying to accomplish. It
may be that 'for_each' is not the best tool for the job.
Aug 29 '06 #6

Thomas J. Gritzan wrote:
learning schrieb:
I am trying to learn STL but got stuck in for_each().
What I intend to do in the following is to make a list with each
element as a string.
add the string elements to the list until it has 10 elements. each
element is the same 1234567890. then I want to use for_each to iterate
the list, and for each to call apply() whihc is create a filen with
name the same as string al; and within each file, it contains the
content of the list element.
I go stuck in making apply to work. Can anyone help?? the following is
the code, it compiles under vc++ 8.

It doesn't compile.
Thanks
#include <algorithm>
#include <list>
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>

using namespace std;

class makefile{

public:
void operator()(std::string sth, std::string sth2){

};
void apply(std::string s, std::string name){

Put this code in operator().

for_each supplies only one argument, so it should be:

void operator()(std::string name) // or:
void operator()(const std::string& name)
std::filebuf buffer;
std::ostream output(&buffer);
buffer.open(name.c_str(), std::ios::out);
output << s << std::endl;

Why don't you use a simple ofstream object?

std::ofstream output(name.c_str());
output << s << std::endl;
}
};

int main(){
std::string s("1234567890");
std::string al("abc");
list<std::stringlString;
for (int i=0; i <= 9; i++) {
//std::string tmp(itoa(i));
al = s+ s.at(i);

What is 'al' for?
lString.push_back(s);
}
//for_each();

for_each(lString.begin(),lString.end(),apply());

for_each(lString.begin(), lString.end(), makefile());
return 0;
}

Your makefile() functor opens a file with name given to it and outputs a
string given as second argument. for_each can only work with one element at
a time.
If you want multiple files with different output strings, try a
std::list< std::pair<std::string, std::string
with first string being the filename and second string the text to output.

The functor would be like this:

struct makefile
{
void operator()(const std::pair<std::string, std::string>& record) const
{
std::ofstream file(record.first.c_str());
file << record.second << std::endl;
}
}

--
Thomas
I was thinking about using "string al" as the filename and "string s",
each list element is a string s, and the play program uses that as the
file content.

My original intention was to change the list element too, but just
leave it this way to make the for_each works first.

so let's say list has 10 elements, each of which is a string:
1234567890 and after the action applied under the for_each (or other
mechanism)..
the filename will be the
1234567890<1>.txt where <1is the counter (the <will not appear in
the filename, just mark here for clairty)
so the expected result will have 10 files:
12345678901.txt
12345678902.txt ....
all has 1234567890 as file content. As you are seeing, I am clearly
weak in STL and good c++.

So further questions on your post:
My original throught was somethign like:
for_each(lString.begin(), lString.end(), makefile(al)); <<---- of
course this does not compile, so I need to take away the al and make
makefile(); .. I am more confused by the std::pair .. how can this
functor be called from for_each?

I think I am not clear about that that are you saying the modified
functor makefile WONT work with for_each because of the restriction for
1 argument functor ? Or that the new modified one WILL work with
for_each?
I don't think the following will compile:
for_each(lString.begin(),lString.end(),makefile(al )); or
for_each(lString.begin(),lString.end(),makefile()) ;
or are there some other tricks?
Thanks

Aug 30 '06 #7

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

Similar topics

5
by: John | last post by:
Hi, I have the following code: <FORM> <font size="3">Brands </font><br /> <SELECT SIZE="1" NAME="categorylist" STYLE="font-size: 8pt"> <OPTION VALUE=http://my.domain,.com/cetegory1.html...
1
by: jg | last post by:
I have searched marshaling articles from MSDN and Google but I am stuck with solving my problem to marshal arrays to COM caller from my managed COM class I built a .net COM class in VB and...
7
by: Lae. | last post by:
I can't figure this one out. n00b question no doubt, this is my first ever JS attempt. Here's the snippet, and here's the full deal http://www.ualberta.ca/~koryb/first.js...
0
by: emiliano | last post by:
Hey guys, i was just googling some information about how to use the ClientForm package with a page which requires HTTP basic authentication and i got here :P ... So here is the problem, lets see if...
16
by: Rex | last post by:
Hi All - I have a question that I think MIGHT be of interest to a number of us developers. I am somewhat new to VIsual Studio 2005 but not new to VB. I am looking for ideas about quick and...
4
by: naknak4 | last post by:
Introduction This assignment requires you to develop solutions to the given problem using several different approaches (which actually involves using three different STL containers). You will...
6
by: naknak | last post by:
Introduction This assignment requires you to develop solutions to the given problem using several different approaches (which actually involves using three different STL containers). You will...
5
by: outofmymind | last post by:
Hi every1, im trying to solve this question, i did some of it but i dont think that its correct or complete: this is the question: Write the definition of a class called Product. A Product...
53
by: souporpower | last post by:
Hello All I am trying to activate a link using Jquery. Here is my code; <html> <head> <script type="text/javascript" src="../../resources/js/ jquery-1.2.6.js"</script> <script...
0
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
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...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
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...
1
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)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
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
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.