#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
bool compare(string a, string b)
{
if (a.length() < b.length())
{
return true;
}
else if (a.length() > b.length())
return false;
else
{
for (int i = 0; i < a.length(); i++)
{
if (a[i] < b[i])
return true;
else if (a[i] > b[i])
return false;
}
return false;
}
}
int main() {
int n{ 0 };
cin >> n;
vector <string> words(n);
for (int i = 0; i < n; i++)
cin >> words[i];
sort(words.begin(), words.end(), compare);
words.erase(unique(words.begin(), words.end()), words.end());
for (int i = 0; i < words.size(); i++)
cout << words[i] << "\n";
}
Sort 메서드에서 comparator로 들어오는 매개함수는 반드시 Strict Weak Ordering을 만족해야 한다.
A == B일 경우에는 A < B도 false이고 A > B도 false여야 한다는 말이다.
또, vector에서 중복을 제거할 때는
words.erase(unique(words.begin(), words.end()), words.end());
erase 와 unique 함수를 사용한다.