方法1:multiset

#include <iostream>
#include <string>
#include <set>
using namespace std;
int main()
{
    int n;
    cin >> n;
    multiset<string> s;
    string str;
    for (int i = 0; i < n; i++) {
        cin >> str;
        s.emplace(str);
    }
    for (auto it = s.begin(); it != s.end(); it++) {
        cout << *it << endl;
    }
}

方法2:algorithm的sort()

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
    int n;
    cin >> n;
    string s;
    vector<string> v;
    while (cin >> s) {
        v.push_back(s);
    }
    sort(v.begin(), v.end());
    for (string str : v) {
        cout << str << endl;
    }
}