L
learning
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:stream output(&buffer);
buffer.open(name.c_str(), std::ios:ut);
output << s << std::endl;
}
};
int main(){
std::string s("1234567890");
std::string al("abc");
list<std::string> lString;
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;
}
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:stream output(&buffer);
buffer.open(name.c_str(), std::ios:ut);
output << s << std::endl;
}
};
int main(){
std::string s("1234567890");
std::string al("abc");
list<std::string> lString;
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;
}