Detailed description of C + + STL full permutation function

Source: Internet
Author: User

First, the concept

Any element of M (m≤n) from n different elements, arranged in a certain order, is called an arrangement of extracting m elements from n different elements. All permutations are called when m=n. If this group has n, then the total number is n!.

For example, the whole arrangement of a,b,c a total of 3! = 6 types are {A, B, C}, {A, C, b}, {B, A, c}, {B, C, a}, {C, a, b}, {C, B, a}.

Second, common operation

1. Header files

#include <algorithm>

  2. How to use

  Here are the first two concepts: "The next permutation combination and the previous permutation, for the sequence {A, B, c}, each element is smaller than the following, according to the dictionary sequence, A is smaller than BC, C is larger than B, its next sequence is {A, C, b}, and the previous sequence of {A, C, b} is {A, B, C}, the same can be introduced for all six sequences: {A, B, C}, {A, C, b}, {B, A, c}, {B, C, a}, {C, a, b}, {C, B, a}, where {A, B, C} have no previous element, {C, B, a} have no next element.

1) Next_permutation: Find the next permutation combination

A. Function Template: Next_permutation (arr, arr+size);
B. Parameter description:
Arr: array Name
Size: Number of array elements
C. function function: The return value is type bool, when the current sequence does not have the next arrangement, the function returns false, otherwise true, the arranged number is stored in the array

D. Note: You need to sort the array in ascending order before use, otherwise you can only find the total number of permutations after the sequence.
For example, if the array num is initialized to 2,3,1, then the output becomes: {2 3 1} {3 1 2} {3 2 1}

2) Prev_permutation: Find the last permutation combination

A. Function Template: Prev_permutation (arr, arr+size);
B. Parameter description:
Arr: array Name
Size: Number of array elements
C. function function: The return value is type bool, and the function returns False if the current sequence does not have a previous arrangement, otherwise it returns true
D. Note: Before use, you need to sort the array in descending order, otherwise you can only find the total number of permutations after the sequence.

Third, the Code

#include <iostream>#include<algorithm>using namespacestd;intMain () {intArr[] = {3,2,1}; cout<<"full arrangement of 3 2 1 with prev_permutation"<<Endl;  Do{cout<< arr[0] <<' '<< arr[1] <<' '<< arr[2]<<'\ n'; }     while(Prev_permutation (arr,arr+3) );///gets the previous large dictionary order, if 3 is changed to 2, only the first two numbers are fully arranged    intArr1[] = {1,2,3}; cout<<"full arrangement of 1 2 3 with next_permutation"<<Endl;  Do{cout<< arr1[0] <<' '<< arr1[1] <<' '<< arr1[2] <<'\ n'; }     while(Next_permutation (arr1,arr1+3) );///gets the next large dictionary order, if 3 is changed to 2, only the first two numbers are fully arranged///Note the array order, and order the arrays first if necessary    return 0;}

Detailed description of C + + STL full permutation function

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.