The arrangement of C++stl

Source: Internet
Author: User

Although we can use recursion to write the full array of programs, but since the STL already has this function why not directly use it, write a direct use of C + + STL to generate a fully sequenced program

Function Name: next_permutation

Include header file: algorithm

Function Prototypes:

Template<class bidirectionaliterator>   

bool Next_permutation (bidirectionaliterator _first, Bidirectionaliterator _last );

Template<class Bidirectionaliterator, class binarypredicate>   

bool Next_permutation (bidirectionaliterator _first, Bidirectionaliterator _last, binarypredicate _comp );

Two overloaded functions, the second with the predicate parameter _comp, with only two arguments in the version, and the default predicate function is "less than".

return value: type bool (default returns False if the current call arrangement reaches the maximum dictionary order, and returns true if the arrangement is dictionary), and the next arrangement is output in the order in which the dictionary is incremented. For example, in the alphabet, the next word of ABCD is arranged in ABDC)

So if you are generating a full array of arrays, first sort the pairs in ascending order, and then use the Do-while statement to loop through the Next_permutation function

1#include <iostream>2#include <algorithm>3#include <string>4 using namespacestd;5 intMain ()6 {7     stringstr;8Cin>>str;9     intlen=str.length ();Ten     Char*cstr= (Char*) Str.c_str (); Onecout<<"arrange the output as follows"<<Endl; A      Do -     { -cout<<cstr<<Endl; the} while(Next_permutation (cstr,cstr+len)); -cout<<"after arranging, the CStr becomes:"<<Endl; -cout<<CStr; -     return 0; +}

Above is a no plus sort direct call nextpermation look at the comparison of output results in case of different inputs

1#include <iostream>2#include <algorithm>3#include <string>4 using namespacestd;5 intMain ()6 {7     stringstr;8Cin>>str;9     intlen=str.length ();Ten     Char*cstr= (Char*) Str.c_str (); OneSort (cstr,cstr+len); Acout<<"arrange the output as follows"<<Endl; -      Do -     { thecout<<cstr<<Endl; -} while(Next_permutation (cstr,cstr+len)); -cout<<"after arranging, the CStr becomes:"<<Endl; -cout<<CStr; +     return 0; -}

Add a sort and look at the effect

The arrangement of C++stl

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.