안녕하세요? 김백일입니다.
지난번(7번) 글에서 AnsiString의 StringReplace와 유사한, string_replace()를 구현했었습니다.
다시 한번 설명하자면, string_replace()는
string에서 특정 시퀀스(서브스트링)를 찾아 다른 시퀀스로 치환한 문자열을 리턴하는 함수입니다.
이러한 기능을 string뿐만 아니라 다른 시퀀스 컨테이너에서도 사용할 수 있도록
제네릭 알고리듬 버전으로 변형했습니다.
알고리듬의 이름은 replace_seq_copy 로 했는데,
'seq'는 sequence를 말하며, 'copy'는 복사 버전이고 in-place 버전이 아니라는 뜻입니다.
replace_seq 라는 이름의 in-place 버전을 만들지 않은 이유는,
복사를 하지 않고 원본 시퀀스 [first1, last1)을 직접 수정하려면
vector와 deque의 경우, 삽입과 삭제가 선형 시간이 걸리는 문제점이 있기 때문입니다.
다음은 replace_seq_copy의 구현 코드입니다.
Binary Predicate를 사용하는 버전도 만들었습니다.
// [first1, last1)에서 서브 시퀀스 [first2, last2)를 찾아 [first3, last3)으로 치환한
// 시퀀스를 result로 출력합니다.
template <typename ForwardIterator1,
typename ForwardIterator2,
typename ForwardIterator3,
typename OutputIterator>
OutputIterator replace_seq_copy(ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2,
ForwardIterator3 first3, ForwardIterator3 last3,
OutputIterator result, bool replace_all)
{
ForwardIterator1 search_pos = first1;
int patt_len = last2 - first2;
while (first1 != last1) {
search_pos = search(search_pos, last1, first2, last2);
if (search_pos != last1) {
copy(first1, search_pos, result);
copy(first3, last3, result);
first1 = search_pos + patt_len;
if (replace_all)
search_pos += patt_len;
else {
copy(first1, last1, result);
break;
}
} else {
copy(first1, last1, result);
break;
}
}
return result;
}
template <typename ForwardIterator1,
typename ForwardIterator2,
typename ForwardIterator3,
typename OutputIterator, typename BinaryPredicate>
OutputIterator replace_seq_copy(ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2,
ForwardIterator3 first3, ForwardIterator3 last3,
OutputIterator result, BinaryPredicate pred,
bool replace_all)
{
ForwardIterator1 search_pos = first1;
int patt_len = last2 - first2;
while (first1 != last1) {
search_pos = search(search_pos, last1, first2, last2, pred);
if (search_pos != last1) {
copy(first1, search_pos, result);
copy(first3, last3, result);
first1 = search_pos + patt_len;
if (replace_all)
search_pos += patt_len;
else {
copy(first1, last1, result);
break;
}
} else {
copy(first1, last1, result);
break;
}
}
return result;
}
다음은 이들 알고리듬을 사용하는 예제 프로그램입니다.
파일에서 한 라인씩 읽어서 특정 문자열을 검색해서 다른 문자열로 치환하는 프로그램입니다.
//---------------------------------------------------------------------------
#include <cstdlib>
#include <iostream>
#include <fstream>
#pragma hdrstop
#include <algorithm>
#include <string>
//---------------------------------------------------------------------------
#pragma argsused
using namespace std;
template <typename ForwardIterator1,
typename ForwardIterator2,
typename ForwardIterator3,
typename OutputIterator>
OutputIterator replace_seq_copy(ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2,
ForwardIterator3 first3, ForwardIterator3 last3,
OutputIterator result, bool replace_all = true);
int main(int argc, char* argv[])
{
if (argc < 4) {
cerr << "Usage: " << argv[0]
<< " input_file output_file old_pattern new_pattern\n";
exit(0);
}
ifstream fin(argv[1]);
ofstream fout(argv[2]);
string old_pattern(argv[3]);
string new_pattern(argv[4]);
string sLine, sReplacedLine;
while(1) {
getline(fin, sLine);
if (!fin.good()) break;
sReplacedLine.clear();
replace_seq_copy(sLine.begin(), sLine.end(),
old_pattern.begin(), old_pattern.end(),
new_pattern.begin(), new_pattern.end(),
back_inserter(sReplacedLine));
fout << sReplacedLine << endl;
}
return 0;
}
//---------------------------------------------------------------------------
|