for_each 함수를 사용하여 파일에 객체를 출력하고 싶습니다. 미친 것처럼 들리지만 가능합니까?. 나는 나 자신을 시도했지만 효과가없는 것 같다.for_each를 사용하여 파일에 데이터 출력 C++?
//Sterling.h
template<class T>
void myfn(const T& t, const iostream& io = cout)
{
io << t;
}
template<class T>
class fefnptr{
public:
void operator()(const T& t, const iostream& io = cout) const
{
io << t;
}
};
class Sterling{
// also implement the operator<< and other functions in Sterling.cpp
};
//main.cpp
int main(){
fstream fp("test",fstream::out);
if(!fp) cerr << "Unable to open the file\n";
else
{
for_each(arr,arr+5,fefnptr<Sterling>(,fp)); // the syntax here is wrong and
I know that but I just want to put the fp as an parameter to output the object to the file
}
fp.close();
return 1;
}
을 그리고 오류 (물론 나는 그것이 무엇인지 알고)없는 매개 변수를 (그것이 내가 파일을 출력 할 개체입니다) 밝혀 : 여기에 지금까지 한 일이다. 그래서 for_each를 사용하여 파일에 객체를 출력하는 방법은 무엇입니까? 미리 감사드립니다. fefnptr
의 생성자에 당신은 통과해야
for_each(arr,arr+5, bind2nd(fefnptr<Sterling>(), fp));
왜 사용할까요'for_each' : 청소기 솔루션은
std::copy
과 스트림 반복자를 사용하는 것입니다? 'std :: ostream_iterator'와 함께'std :: copy'를 사용하지 않을까요? –