[10주완성 C++ 코딩테스트] Split

Taegang Yun·2023년 11월 27일
1

split함수가 뭐에요?

예를 들어

"aaa bbb ccc" 문자열이 있는데 내가 이걸 띄어쓰기를 기준으로 나눠버리고 싶다.
배열에 {aaa, bbb, ccc} 이렇게 담기게 하는 것이다.

다음 3줄만 외우면 된다.


while((pos = input.find(delimeter)) != string::npos){
	token = input.substr(0, pos);
    ret.push_back(token);
    input.erase(0, pos + delimeter.length());
}

전체 코드


vector<string> split(string input, string delimiter){
	vector<string> ret;
    long long pos = 0;
    string token = "";
  	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;
}
profile
언젠간 전문가가 되겠지

0개의 댓글