stl에서 split를 이용할적에 delimiters에 언다바(_)가 들어가면

stypr의 이미지

제목처리 delimiters 를 _AA_이런 형식으로 주면
문자열이 다음처럼 언더바가 들어가면 언더바까지 split를 하고만는군요. 실제 split는 언더바 몇번 뒤에있는데 말입니다.

아래에서 _AA_로 split하면
aaa_bbb_ccc
ddd
이렇게 나와야하는데....이거이..
aaa
bbb
ccc
이렇게 나오는군요.
_AA_aaa_bbb_ccc_AA_ddd

find_first_of를 이용하여 pos를 엊는데 pos값의 범위가 이상하군요..흑흑..

feanor의 이미지

STL에는 split이 없습니다만? 무슨 말씀이신지?

그리고 문자열을 검색하려면 find 쪽이 아니라 search 쪽을 써야 할 텐데요. find는 문자열이 아니라 문자 단위로 검색할걸요.

--feanor

yielding의 이미지

예전의 블루스 에켈의 책에서 봤던 거 같은데

void 
split(const string& line, const string& separators, list<string>& tokens) 
{
    size_t start = line.find_first_not_of(separators);
    size_t end   = line.length();

    while ((start >=0) && (start < end)) {
            size_t stop = line.find_first_of(separators, start);
            if ((stop < 0) || (stop > end)) stop = end;
            tokens.push_back(line.substr(start, stop-start));
            start = line.find_first_not_of(separators, stop+1);
    }
}

list대신에 vector나 다른 컨테이너 써도 되겠죠..
그리고 wstring을 고려한다면

template<typename T>
void 
split(const T& line, const T& separators, list<T>& tokens) 
{
    size_t start = line.find_first_not_of(separators);
    size_t end   = line.length();

    while ((start >=0) && (start < end)) {
         size_t stop = line.find_first_of(separators, start);
         if ((stop < 0) || (stop > end)) stop = end;
         tokens.push_back(line.substr(start, stop-start));
         start = line.find_first_not_of(separators, stop+1);
    }
}

list<wstring> r;
wstring s=L"1,2,3,4,5";
split<wstring>(s, L"," r);
이렇게도 쓸 수 있겠습니다.

Life rushes on, we are distracted

feanor의 이미지

test님이 원하시는 것은

split("_AA_aaa_bbb_ccc_AA_ddd", "_AA_", list)
해서 list에 aaa_bbb_ccc와 ddd가 들어가는 걸 원하시는 것 같은데,
yielding님이 하신 방법으로 하면 그렇게 안 됩니다.

find_first_of에 넘어가는 인자는 seperator*s* 입니다. _AA_가 문자열
_AA_를 나타내는 것이 아니라, _, A, A, _ 이 네 글자 중 하나가 나오면 split
하라는 뜻으로 해석됩니다.

--feanor

feanor의 이미지

// split.cc

#include <iostream>
#include <string>
#include <vector>

using namespace std;

void split(const string& line,
           const string& separators,
           vector<string>& tokens)
{
    size_t start = line.find_first_not_of(separators);
    size_t end = line.length();

    while ((start >= 0) && (start < end)) {
        size_t stop = line.find_first_of(separators, start);
        if ((stop < 0) || (stop > end)) stop = end;
        tokens.push_back(line.substr(start, stop-start));
        start = line.find_first_not_of(separators, stop+1);
    }
}

void split2(const string& line,
            const string& separator,
            vector<string>& tokens)
{
    size_t seplen = separator.length();
    size_t start = line.find(separator);
    size_t end = line.length();

    while ((start >= 0) && (start < end)) {
        start = start + seplen;
        size_t stop = line.find(separator, start);
        if ((stop < 0) || (stop > end)) stop = end;
        tokens.push_back(line.substr(start, stop-start));
        start = stop;
    }
}

int main(void)
{
    vector<string> l;
    split("_AA_aaa_bbb_ccc_AA_ddd", "_AA_", l);
    cout << l[0] << " " << l[1] << endl;

    vector<string> l2;
    split2("_AA_aaa_bbb_ccc_AA_ddd", "_AA_", l2);
    cout << l2[0] << " " << l2[1] << endl;
}

$ g++ -o split split.cc
$ ./split
aaa bbb
aaa_bbb_ccc ddd
$

잘 비교해보세요. ^^

--feanor

yielding의 이미지

질문을 정확히 이해하지 않고 답을 달았군요 feanor의 깔끔한 답에 한표 ^^;

Life rushes on, we are distracted

kghoon의 이미지

strtok의 경우처럼 separator를 기준으로 그 앞에 토큰까지 취하고 싶다면..

void 
mysplit(const string& line, const string& separator, vector<string>& tokens) 
{
    size_t seplen = separator.length();
    size_t start = 0;
    size_t end = line.length();
 
    while ((start >= 0) && (start < end)) {
        start = start + seplen;
        size_t stop = line.find(separator, start);
        if ((stop < 0) || (stop > end)) stop = end;
        tokens.push_back(start==seplen ? line.substr(0, stop)  : line.substr(start, stop-start));
        start = stop;
    }
}

///////////////////

string bcd= "aa^bb^cc^dd^ee";
 
vector<string> v;
mysplit(bcd, "^", v);
for(vector<string>::iterator _itr=v.begin(); _itr!=v.end(); ++_itr)
{
	cout << (*_itr) << endl;
}

///////////////////

aa
bb
cc
dd
ee

Engineer at Alticast

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.