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