Q.
-------------
홍길동
박철구
박철수
김영숙
아무개
홍길동
박철구
박철수
김영숙
아무개
-------------
라고 저장이 되어있는 파일이 있습니다.
여기서 "아무개"를 전부 삭제하여,
-------------
홍길동
박철구
박철수
김영숙
홍길동
박철구
박철수
김영숙
-------------
라는 파일을 만드려면 어떻게 해야 하나요?
(단, 메모리에는 한 번에 한 단어만 로드할 수 있습니다.)
A.
텍스트 파일을 통째로 메모리에 올려둘 수 있다고 한다면
각 단어를 list 컨테이너에 전부 로드한 뒤,
remove 알고리듬을 사용하여 삭제하신 후,
다시 저장하면 됩니다.
그러나, 텍스트가 너무 커서 메모리에 전부 로드할 수 없다면,
다음과 같이
입출력 반복자인 istream_iterator, ostream_iterator와
remove_copy 알고리듬을 사용하는 방법이 있습니다.
이 경우는 입력 파일과 출력 파일을 다르게 해야합니다.
//---------------------------------------------------------------------------
// remove_copy.cpp
//---------------------------------------------------------------------------
#include <cstdlib>
#include <iostream>
#include <fstream>
#pragma hdrstop
#include <string>
#include <iterator>
#include <algorithm>
//---------------------------------------------------------------------------
using namespace std;
#pragma argsused
int main(int argc, char* argv[])
{
if (argc < 3) {
cerr << "usage: remove_copy inputfile outputfile\n";
exit(1);
}
ifstream fin(argv[1]);
ofstream fout(argv[2]);
istream_iterator<string> end_of_stream;
remove_copy(istream_iterator<string>(fin), end_of_stream,
ostream_iterator<string>(fout, "\n"), string("아무개"));
return 0;
}
//---------------------------------------------------------------------------
|