str 파싱하는 방법 (str.find)

강신현·2022년 7월 10일
0

str.find() 를 사용하여 특정 문자를 찾아 그 문자 기준으로 문자열을 파싱할 수 있다.

예제

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

string str;
vector<string> ans;

int main(){
    
    cin >> str;
    
    int pos = str.find("_");

    while(pos != string::npos){
        if(str.substr(0,pos) != "") ans.push_back(str.substr(0,pos)); // 0 ~ pos까지의 부분 문자열
        str.erase(0,pos+1); // pos+1 전까지의 문자들을 지운다

        pos = str.find("_");
    }
    ans.push_back(str);

    for(int i=0;i<ans.size();i++){
        cout << i+1 << "#" << ans[i] << "\n";
    }

    return 0;
}
profile
땅콩의 모험 (server)

0개의 댓글

관련 채용 정보