Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- @environmentobject 프로퍼티 래퍼
- react-quill 경고메세지
- accesstoken 재발급
- react hook
- C++
- 비동기함수
- finddomnode경고
- react modal
- featured-sliced-design
- 리렌더링최적화
- git
- CSS
- zustand란
- BFS
- react fsd
- zustand
- react상태관리라이브러리
- 동기 함수 내에서 비동기 함수 호출
- @observedobject 프로퍼티 래퍼
- @published 프로퍼티 래퍼
- 리액트최적화
- jwt프론트
- 원격저장소 연결
- 페이지이동함수
- 컴퓨터네트워크
- react-quill
- modal 관리
- react-router-dom
- jwt로그아웃
- gitbub desktop
Archives
- Today
- Total
leebaek
[c++] stringstream - 공백 문자열을 처리하는 방법 본문
stringstream
문자열과 숫자를 다룰 때 유용한 도구
< 사용 상황 정리 >
1. 숫자를 문자열로 변환할 때
2. 문자열에서 숫자 추출할 때
3. 공백 기준으로 단어 분할할 때
#include <sstream>
stringstream ss; // 빈 스트림 선언
ss << "hello world!" // 문자열 추가해줘야 함
stringstream ss(input) // 초기값을 가진 스트림 선언
ss >> word >> number; // 공백 기준 순서대로 데이터 추출
- ss >> 연산자는 자동으로 타입을 감지하여 변환함
1. [ 숫자를 문자열로 변환 ]
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
// to_string()과 같은 역할
int main() {
int num = 123;
std::stringstream ss;
ss << "정수 " << num;
string str = ss.str(); // 문자열로 변환
cout << str;
}
[문자열을 공백 기준으로 토큰화] - 2. [ 문자열을 숫자로 변환 ]
- 프로그래머스 | 최댓값과 최솟값
https://school.programmers.co.kr/learn/courses/30/lessons/12939
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
int main() {
vector<int> numbers;
string input = "-11 1 1";
int num;
std::stringstream ss(input);
while ( ss >> num ) {
numbers.push_back(num);
}
for ( int num: numbers )
cout << num << ' ';
}
[문자열을 공백 기준으로 토큰화] - 3. [ 단어 분할 ]
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
int main() {
string input = "Hello world!";
stringstream ss(input);
string word;
while ( ss >> word ) {
cout << "단어 : " << word;
}
}
'프로그래밍 언어 > C++' 카테고리의 다른 글
[c++/함수] unique (0) | 2023.11.02 |
---|---|
[c++ STL] pair (0) | 2023.09.05 |