Finding the longest connection path of a string

Topic describes
Given n strings, arrange n strings in lexicographic order.
The input describing
Enter the first action with a positive integer N (1≤n≤1000), and the following action with n strings (string length ≤100), which contain only upper and lower case letters.
Output description
The data outputs n lines of a lexicographical string.
The sample
Type :
9
cap
to
cat
two
too
up
boat
boot

boat
boot
cap
card
cat
to
too
two
up
Train of thought
The container Set can insert elements into the container at any time, quickly find elements at any time, and need to be traversed in some order. Multiset is an extension of set. Compared with set, multiset contains duplicate elements, that is, set considers key-like elements as the same element, and for counting +1, multiset considers key-like elements as equivalent elements and stores them in turn. Insert the input word into the container multiset in some order, and then print out the elements in the container.

#include <iostream>
#include <set>
using namespace std;
int main(){
    int n;
    cin>>n;
    string input;
    multiset<string> arr;
    while(n--){
        cin>>input;
        arr.insert(input);
    }
    for(auto myarr:arr){
        cout<<myarr<<endl;
    }
    return 0;
}

Using the container Vector, input strings are pressed into the container one by one, and the elements in the container are sorted after all are pressed.

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool compare(string str1,string str2){
    return str1<str2;
}
int main(){
    int n;
    cin>>n;
    string input;
    vector<string> arr;
    while(n--){
        cin>>input;
        arr.push_back(input);
    }
    sort(arr.begin(),arr.end(),compare);
    for(int i=0;i<arr.size();i++){
        cout<<arr[i]<<endl;
    }
    return 0;
}

Read More: