본문 바로가기

C++

[C++] string TO vector string


string 형태의 문자열에서 delimeter(구분자)를 기준으로 문자열을 분할하여 vector string으로 변환한다.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Convert string to vector string
// [INPUT]
//   >> const std::string& _s                input string divied by [delimeter _c]
//   >> const char                            delimeter
// [OUTPUT]
//   >> std::vector<std::string>& vs_       output vector string
void    to_vs    ( const std::string& _s, std::vector<std::string>& vs_, const char _c)
{
    t::s s;
    vs_.clear();
    for( size_t i(0); i<_s.size(); i++ )
    {
        char c(_s[i]);
        //if(c==_c && s.length() != 0)
        if(c==_c)
        {
            vs_.push_back(s);
            s="";
        }
        else s+=c;
    }
    if ( s.length() != 0 )
        vs_.push_back(s);
}
cs


'C++' 카테고리의 다른 글

[C++] 현재 시간 얻기  (0) 2016.10.24
[C++] file TO vector string  (0) 2016.10.13
[C++] 폴더 내 파일목록 읽기  (0) 2016.04.13