250x250
Notice
Recent Posts
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
관리 메뉴

일상 코딩

[C++/CPP] 18.03. 문자열 stream 본문

C++/따배C++ 18강 입력과 출력

[C++/CPP] 18.03. 문자열 stream

polarcompass 2021. 12. 18. 23:59
728x90
#include<iostream>
#include<sstream>

using namespace std;

int main(int argc, char const *argv[])
{
    {
        stringstream os;

        os << "hello, world!"; // << : insertion operator,
                               // >> : extraction operator
        // os.str("Hello, World!");
        os << "HEllo, World2!" << endl;
        os.str("Hello, World!");
        os.str("Hello, World!\n");

        string str;

        // os >> str; // 빈칸을 만나면 끊기게됨.

        str = os.str(); // 문자열 전체 출력됨

        cout << str << endl;
    }

    {
        stringstream os;

        os << "12345 67.89";

        string str1;
        string str2;

        os >> str1 >> str2;

        cout << str1 << " | " << str2 << endl;

    }

    {
        stringstream os;

        int i = 12345;
        double d = 67.89;

        os << i << " " << d;

        string str1;
        string str2;

        os >> str1 >> str2;

        cout << str1 << " | " << str2 << endl;
    }

    {
        stringstream os;

        os << "12345 67.89";

        int i2;
        double d2;

        os >> i2 >> d2;

        cout << i2 << " | " << d2 << endl;
    }

    {   // os를 비울때
        stringstream os;
        os << "12345 67.89";

        // 전부 지울려고하면 os.str("") 먼저 한 후 os.clear() 시행
        // os.str("");

        // 숫자만 남기고 문자만 지움 
        os.clear(); // __state 까지 초기화함.

        os << "Hello";

        cout << os.str() << endl;

    }

    return 0;
}
728x90