https://life-with-coding.tistory.com/403

 

[C++] stringstream 사용법

인트로 안녕하세요. 오늘은 C++의 Stringstream 사용법에 대해 포스팅하겠습니다. C++에서 여러가지 자료형이 한 줄에 들어오면 파싱해서 용도에 맞게 사용할 필요가 있는데요. 특히 "이름 날짜 내용"

life-with-coding.tistory.com

상기블로그 내용 그대로 따라하며 익히기.

#include<iostream>
#include<sstream>
#include<string>
#include<vector>
using namespace std;
typedef long long ll;
int main(int argc, char** argv) {
    /*int num;
    stringstream msg;
    msg.str("123 456");
    while (msg >> num) {
        cout << num << endl;
    }*/

    //2.  msg초기화
   /* int num;
    stringstream msg;
    msg.str("123 456");
    while (msg >> num) {
        cout << num << endl;
        msg.str("");
    }*/

    //3. stringstream 활용해 날짜를 초로 바꾸기.

    vector<ll> time;
    string str = "2022:12:04 09:47:00";
    for (int x = 0; x < str.size(); ++x) {
        if (str[x] == ':')
            str[x] = ' ';
    }

    ll num = 0;
    stringstream msg;
    msg.str(str);
    while (msg >> num) {
        time.push_back(num);
    }
    ll second = 0;
    second += time[0] * 365 * 24 * 60 * 60; //  연
    second += time[1] * 30 * 24 * 60 * 60; //월
    second += time[2] * 24 * 60 * 60; // 일
    second += time[3] * 60 * 60; //시
    second += time[4] * 60; //분
    second += time[5];//초

    cout << second << endl;
}

https://roadtosuccess.tistory.com/83

 

[C++] stringstream 사용법(feat. stream과 버퍼란 무엇인가?)

C++에서 문자열을 공백과 \n 을 기준으로 int형 string형 float형 등 다양하게 자를 수 있도록 하는 stringstream이 존재한다. 이것을 어떻게 쓰는지 알아보도록 하자. stringstream을 설명하기 전에 stream은

roadtosuccess.tistory.com

스트림, 버퍼 에 대해 다룬 블로그

 

 


hello world0 ~9 까지 stringstream 이용해서 출력하기

#include<iostream>
#include<sstream>
#include<string>
#include<vector>
using namespace std;
typedef long long ll;
int main(int argc, char** argv) {
	stringstream ss;
	int cnt = 0;
	
	while (cnt < 10) {
		ss << "hello world" << cnt<<" ";
		cout<< ss.str()<<endl;
		ss.str("");
		cnt += 1;
	}
}

'C_C++' 카테고리의 다른 글

google coding style  (0) 2022.12.17
namespace 에 대해  (0) 2022.12.05
클래스 상속  (0) 2022.11.27
main( ) 매개변수 argc, argv 관련  (0) 2022.09.29
한글 각각 출력하기  (0) 2022.09.23

+ Recent posts