Sort container elements

Source: Internet
Author: User

# Include <list> # include <vector> # include <string> # include <algorithm> # include <iterator> # include <iostream> using namespace std; bool isShorter (const string & s1, const string & s2) {return s1.size () <s2.size ();} bool GT6 (const string & s) {return s. size ()> = 6;} string make_plural (size_t ctr, const string & word, const string & ending) {return (ctr = 1 )? Word: word + ending; // make_plural (wc, "word", "s") when the number of words in the input Chinese text is greater than one, s is added after word, it is the plural word of words !} Int main () {// words: the quick red fox jumps over the slow red turtlevector <string> words; vector <string >:: iterator noUnique; // Add words to vectorwords. push_back (string ("the"); words. push_back (string ("quick"); words. push_back (string ("red"); words. push_back (string ("fox"); words. push_back (string ("jumps"); words. push_back (string ("over"); words. push_back (string ("the"); words. push_back (string ("slow"); words. push _ Back (string ("red"); words. push_back (string ("turtle"); // cout <"before sort:" <endl; cout <"------------------------" <endl; for (vector <string >:: iterator iter = words. begin (); iter! = Words. end (); ++ iter) {cout <* iter <endl;} cout <"----------------------------" <endl; // sort and then output sort (words. begin (), words. end (); cout <"after sort:" <endl; cout <"--------------------------" <endl; for (iter = words. begin (); iter! = Words. end (); ++ iter) {cout <* iter <endl;} cout <"----------------------------" <endl; // remove duplicates, then output noUnique = unique (words. begin (), words. end (); // copy non-repeating elements to the front end of the sequence, and the returned iterator points to the words at the next location beyond the end of the non-repeating element range. erase (noUnique, words. end (); // remove duplicate elements cout <"after unique:" <endl; cout <"----------------------------" <endl; for (iter = words. begin (); iter! = Words. end (); ++ iter) {cout <* iter <endl;} cout <"----------------------------" <endl; // sort by the number of characters, find the number of elements with a length greater than 6 and output stable_sort (words. begin (), words. end (), isShorter); vector <string >:: size_type wc = count_if (words. begin (), words. end (), GT6); cout <wc <"<make_plural (wc," word "," s ") <"6 characters or longer" <endl; for (iter = words. begin (); iter! = Words. end (); ++ iter) {if (GT6 (* iter) {cout <* iter <endl ;}} cout <"------------------------" <endl; return 0 ;}

 

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.