LV1 - 문자열내마음대로정렬하기

1. sort 함수 이용하여 strings 정렬하되 사용자정의 함수인 'cmp()' 사용 
2. 비교 함수 cmp를 만들어 조건에 맞게 되있으면 true, 아니면 false
 
- Code
 
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
 
int N;
vector<string> solution(vector<string> strings, int n);
bool cmp(string a, string b);
 
int main()
{
    vector<string> s = { "abce", "abcd", "cdx" };
    solution(s,2);
    return 0;
}
 
vector<string> solution(vector<string> strings, int n) {
    N = n;
    sort(strings.begin(), strings.end(), cmp);
    return strings;
}
 
bool cmp(string a, string b)
{
    if (a[N] == b[N])
    {
        return a < b;
    }
    else
    {
        return a[N] < b[N];
    }
}

댓글을 작성해보세요.