Full Array (next_permutation)

Source: Internet
Author: User

SLT:

C + + STL has a function that can easily generate a full array, which is next_permutation

Look at the function declaration of Next_permutation in C + + reference:

#include <algorithm>
BOOL Next_permutation (iterator start, iterator end);

The next_permutation () function attempts to transform the given range of elements [Start,end] into the next lexicograp hically greater permutation of elements. If It succeeds, it returns true, otherwise, it returns false.

You can see from the description that the return value of Next_permutation is a Boolean type. Follow the prompts to write a standard C + + program:

1#include <iostream>2#include <algorithm>3#include <string>4  5 using namespacestd;6  7 intMain ()8 {9     stringstr;TenCIN >>str; One sort (Str.begin (), Str.end ()); Acout << str <<Endl; -      while(Next_permutation (Str.begin (), Str.end ())) -     { thecout << str <<Endl; -     } -     return 0; -}

The sort function and String.begin (), String.end () are also used, and the function declarations are as follows:

#include <algorithm>
void sort (iterator start, iterator end);

The sort function can use the complexity of NLOGN to sort the data in the parameter range.

#include <string>
Iterator begin ();
Const_iterator begin () const;

#include <string>
Iterator End ();
Const_iterator end () const;

String.begin () and String.end () can quickly access the first and trailing characters of a string.

When using big data tests, it was found that standard C + + was inefficient ... To write a C function, the efficiency has increased more than one times ...

1#include <cstdio>2#include <algorithm>3#include <cstring>4 #defineMAX 1005  6 using namespacestd;7  8 intMain ()9 {Ten     intlength; One     CharStr[max]; A gets (str); -Length =strlen (str); -Sort (str, str +length); the puts (str); -      while(Next_permutation (str, str +length)) -     { - puts (str); +     } -     return 0; +}

Reprint Link: https://www.slyar.com/blog/stl_next_permutation.html

Full Array (next_permutation)

Related Article

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.