[C ++] sorting string Vectors
Let the string vector first sort by the string length, the short before, the long after. If the length is equal, sort by lexicographically and remove duplicate strings.
Repeat and sort by lexicographically:
Void elimDumps (vector
& Words) {// sort (words. begin (), words. end (); // unique sorts the input range so that each word appears only once // and is arranged in front of the range, returns the iterator auto end_unique = unique (words. begin (), words. end (); // Delete duplicate words. erase (end_unique, words. end ());}
Comparison function, used to sort words by length:
Bool isShorter (const string & s1, const string & s2) {return s1.size () <s2.size ();}
Main function:
Int _ tmain (int argc, _ TCHAR * argv []) {// create and initialize the string vector
Words {"aaa", "c", "eeee", "B", "cccc", "c "}; // remove duplicate words and sort elimDumps (words) in Lexicographic Order; // sort the vectors by string size and use the stable Sorting Algorithm to sort words of the same length in Lexicographic Order. begin (), words. end (), isShorter); for (auto & s: words) {cout <s <endl;} return 0 ;}
Program Execution result: