최댓값과 최솟값

magicdrill·2024년 12월 17일
0

최댓값과 최솟값

공백 포함 문자열을 정수의 집합으로 파싱하는 법을 연습한다.
sstream을 사용해 문자열을 파싱한다.

#include <string>
#include <vector>
#include <string>
#include <sstream>
#include <iostream>
#include <algorithm>

using namespace std;

string solution(string s) {
    string answer = "";
    vector<int> numbers;
    stringstream ss(s);//문자열 스트림 생성
    int num;
    
    while(ss >> num){
        numbers.push_back(num);
    }
    cout << "파싱결과 : ";
    for(int num : numbers){
        cout << num << " ";
    }
    cout << "\n";
    
    sort(numbers.begin(), numbers.end());
    cout << "정렬결과 : ";
    for(int num : numbers){
        cout << num << " ";
    }
    cout << "\n";
    
    answer += to_string(numbers.front());
    answer += " ";
    answer += to_string(numbers.back());
    
    return answer;
}

0개의 댓글