기타/C++ 문법

<stringstream> 공백으로 문자열 자르기

배발자 2022. 1. 13.

목차

    반응형
     

    '남이 읽는 CS/운영체제' 카테고리의 글 목록

     ...

    baebalja.tistory.com

     

    운영체제 CS 준비하시는 분들 도움 되셨으면 하는 마음으로 포스팅하고 있어요!!
    관심 있으신 분들은 위의 링크 클릭!

     


     

    문자열이 공백이 주어진 상태로 주어진다면 공백을 기준으로 문자열을 자를 수 있다. 

     

    예)  string s = "abc def gh" ; 

     

    해당 문자열을 "abc" , "def" , "gh" 로 자르는 거다. 

    해당 문법을 알아보자.

    
      
    #include <iostream>
    #include <sstream>
    using namespace std;
    int main() {
    string x, y, z;
    string s1 = "abc def gh";
    stringstream s2(s1);
    s2 >> x >> y >> z;
    cout << x << "\n" << y << "\n" << z;
    }

    <stringstream> 공백으로 문자열 자르기

    
      
    #include <iostream>
    #include <sstream>
    using namespace std;
    int main() {
    string s = "abc def ghi jkl mn opqr stu vw xyz";
    stringstream ss(s);
    string s1;
    while (ss>>s1) {
    cout << s1 << endl;
    }
    cout << "끝";
    }

    <stringstream> 공백으로 문자열 자르기

     

    반응형