[백준 12605] 단어순서 뒤집기
2023. 2. 18. 00:14ㆍ자료구조 및 알고리즘/백준
728x90
getline은 뭐하는 함수인가
공백 구분 받아서 출력하기 쓰기
1) 구분자가 있는 경우 split으로 잘라 vector 에 넣기
#include<iostream>
#include<string>
#include<vector>
#include<sstream>
using namespace std;
int main()
{
string str="java c c++ python";
istringstream ss(str);
string stringBuffer;
vector<string> x;
x.clear();
cout<<"어떻게 잘리는지 확인해봅시다 ->";
//구분자가 , 이라면 getline(ss, stringBuffer, ',')쓰면됨
while (getline(ss, stringBuffer, ' ')){
x.push_back(stringBuffer);
cout<<stringBuffer<<" ";
}
cout<<endl<<"vector 값을 출력해보자."<<endl;
for(int i=0;i<x.size();i++){
cout<<x[i]<<endl;
}
return 0;
}
vector 함수 clear는? https://codingdog.tistory.com/entry/c-vector-clear-size%EB%A5%BC-0%EC%9C%BC%EB%A1%9C-%EB%A7%8C%EB%93%A4%EC%96%B4-%EC%A4%80%EB%8B%A4
stack에도 clear 같은 애 있는지 찾기
stack 버전으로 구현해보기 참고 자료
cin과 getline을 같이 사용할때 cin.ignore()이 필요한 이유 기록
제대로 알지 못하면서 알고 있다고 생각하는것만큼 무서운것이 없습니다. 선무당이 사람 잡는다. cin과 getline을 같이 사용할때 cin.ignore()이 필요한 이유를 잘못 알고 쓰고 있었습니다. 잘못된 이
namwhis.tistory.com
내 코드
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int test;
stack<string> st;
string str;
string token;
cin >> test;
cin.ignore();
for (int i = 1; i <= test; ++i) {
getline(cin, str);
stringstream sstream(str);
while (getline(sstream, token, ' ')) st.push(token);
// sstream을 token으로 나눠주는데 토큰은 ' '(공백)
cout << "Case #" << i << ": ";
while (!st.empty()) {
token = st.top();
st.pop();
cout << token << ' v';
}
cout << '\n';
}
}
728x90
'자료구조 및 알고리즘 > 백준' 카테고리의 다른 글
[백준 11866번] 요세푸스 문제 0 (0) | 2023.02.25 |
---|---|
[백준 9012번] 괄호 (0) | 2023.02.18 |
[백준 4949] 균형잡힌 세상 (0) | 2023.02.18 |
[백준 17608]막대기 문제 최댓값 갱신 원리 및 논리 파악하기! (0) | 2023.02.16 |
백준 C++ 스킬) 문자열 비교하기 (0) | 2023.02.03 |