[백준 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 버전으로 구현해보기 참고 자료

입력 버퍼를 일단 비워준다) https://namwhis.tistory.com/entry/cin%EA%B3%BC-getline%EC%9D%84-%EA%B0%99%EC%9D%B4-%EC%82%AC%EC%9A%A9%ED%95%A0%EB%95%8C-cinignore%EC%9D%B4-%ED%95%84%EC%9A%94%ED%95%9C-%EC%9D%B4%EC%9C%A0-%EA%B8%B0%EB%A1%9D

 

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