[C ++] sorting string Vectors

Source: Internet
Author: User

[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:

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.