split() 메소드 구현

☝🏻 Split() 메소드

c++에서는 split()메소드를 지원하지 않는다. 즉, 구현을 해야한다.

// #include <algorithm>
#include <algorithm>
#include <string>
#include <vector>
#include <iostream>
using namespace std;

vector<string> split(string input,string delimiter){
    long long pos;
    string token = "";
    vector<string> ret;
    while((pos = input.find(delimiter)) != string::npos ){
        token = input.substr(0,pos);
        ret.push_back(token);
        input.erase(0,pos+delimiter.length());
    }
    ret.push_back(input);
    return ret;
}

int main() {
    string s = "엄준식 입니다.";
    vector<string> a = split(s, " ");
    for(string b:a) cout << b << "\n";
    return 0;
}

Categories:

Updated:

Leave a comment