Let the string vector be sorted first by the length of the string, short in front and long in the back. If the length is equal, sort by dictionary order and remove the duplicate string.
To repeat and sort by dictionary order:
void Elimdumps (vector<string> &words) {//sorted by dictionary order sort (Words.begin (), Words.end ());//unique reflow input range, Causes each word to appear only once//and in the front of the range, returning an iterator that points to a location after the non-repeating region auto End_unique = unique (Words.begin (), Words.end ());//delete duplicate words 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 string vectors vector<string> words{"AAA", "C", "Eeee", "B", "CCCC", "C"};//shift In addition to repeating words and sorting by dictionary order elimdumps (words);//Sort vectors by string size, use the stable sort algorithm to keep words of the same length sorted by dictionary order Stable_sort (Words.begin (), Words.end (), Isshorter); for (auto &s:words) {cout << s << endl;} return 0;}
Program execution Results:
[C + +] sort string vectors