필드의 폭이 고정된 경우는,
std::string::substr() 이나 vcl::AnsiString::SubString() 으로
손쉽게 구현할 수 있지만,
비슷한 코드를 중복해서 나열하거나, 루프를 사용해야 하므로
코드가 그리 보기 좋지는 않습니다.
이러한 경우의 토크나이징(?)도 boost::tokenizer의
offset_seperator를 사용하면 깔끔하게 처리할 수 있습니다.
#include<iostream>
#pragma hdrstop
#include<boost/tokenizer.hpp>
#include<string>
int main(){
using namespace std;
using namespace boost;
string s = "12252001";
int offsets[] = {2, 2, 4};
offset_separator f(offsets, offsets + 3);
tokenizer<offset_separator> tok(s, f);
// 루프를 사용하는 방법
for (tokenizer<offset_separator>::iterator i = tok.begin();
i != tok.end(); ++i)
cout << *i << "\n";
// copy 알고리듬을 사용하는 방법
copy(tok.begin(), tok.end(), ostream_iterator<string>(cout, "\n"));
return 0;
}
출력은 다음과 같습니다.
12
25
2001
12
25
2001
|