문자열(string)으로 입력받기, 문자열 쪼개기::string stream
2019. 7. 9. 15:44ㆍComputerScience/C-C++
sstream = string + stream
ios,iostream의 함수들을 상속받아 사용가능 (>>, << ,.eof(),,,)
PS를 풀다보면 문자열을 끊어써야할때가있는데 유용할게 사용가능 ( e.g)12 + 34 )
예제)
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main() {
string input;
getline(cin, input);
stringstream ss(input);
while (!ss.eof()) {
string s;
ss >> s;
cout << s << "\n";
}
/*ss에는 EOF가 처음으로 들어가있어서 출력안됨*/
int k;
ss.str("12 34 56");
while (ss >> k) {
cout << k << "\n";
}
int n;
stringstream sss;
sss.str("12 34 56");
while (sss >> n) {
cout << n << "\n";
}
}
결과
'ComputerScience > C-C++' 카테고리의 다른 글
[gcc/g++] long double operate error (0) | 2021.10.01 |
---|---|
vector 선언,초기화와 정적 배열 초기화 속도차이 (0) | 2019.09.30 |
자잘한 문법들 (0) | 2019.07.25 |
C++17 Structured binding (0) | 2019.07.25 |
클래스 템플릿// 멤버함수의 외부 선언 (0) | 2019.05.12 |