Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 75895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T20:35:16+00:00 2026-05-10T20:35:16+00:00

std::next_permutation (and std::prev_permutation) permute all values in the range [first, last) given for a

  • 0

std::next_permutation (and std::prev_permutation) permute all values in the range [first, last) given for a total of n! permutations (assuming that all elements are unique).

is it possible to write a function like this:

template<class Iter> bool next_permutation(Iter first, Iter last, Iter choice_last); 

That permutes the elements in the range [first, last) but only chooses elements in the range [first, choice_last). ie we have maybe 20 elements and want to iterate through all permutations of 10 choices of them, 20 P 10 options vs 20 P 20.

  • Iter is a random access iterator for my purposes, but if it can be implemented as a bidirectional iterator, then great!
  • The less amount of external memory needed the better, but for my purposes it doesn’t matter.
  • The chosen elements on each iteration are input to the first elements of the sequence.

Is such a function possible to implement? Does anyone know of any existing implementations?

Here is essentially what I am doing to hack around this. Suggestions on how to improve this are also welcome.

  • Start with a vector V of N elements of which I want to visit each permutation of R elements chosen from it (R <= N).
  • Build a vector I of length R with values { 0, 1, 2, ... R - 1 } to serve as an index to the elements of V
  • On each iteration, build a vector C of length R with values { V[I[0]], V[I[1]], ... V[I[R - 1]] }
  • Do something with the values in C.
  • Apply a function to permute the elements of I and iterate again if it was able to.

That function looks like this:

bool NextPermutationIndices(std::vector<int> &I, int N) {     const int R = I.size();     for (int i = R - 1; ; --i) {         if (I[i] < N - R + i) {             ++I[i];             return true;         }          if (i == 0)             return false;          if (I[i] > I[i-1] + 1) {             ++I[i-1];             for (int j = i; j < R; ++j)                 I[j] = I[j-1] + 1;             return true;         }     } } 

That function is very complicated due to all the possible off-by-one errors, as well everything using it are more complicated than is probably necessary.


EDIT:

It turns out that it was significantly easier than I had even imagined. From here, I was able to find exact implementations of many of the exact algorithms I needed (combinations, permutations, etc.).

template<class BidirectionalIterator> bool next_partial_permutation(BidirectionalIterator first,                               BidirectionalIterator middle,                               BidirectionalIterator last) {     std::reverse(middle, last);     return std::next_permutation(first, last); } 

Plus there is a combination algorithm there that works in a similar way. The implementation of that is much more complication though.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T20:35:17+00:00Added an answer on May 10, 2026 at 8:35 pm

    To iterate over nPk permutations, I’ve used the for_each_permutation() algorithm presented in this old CUJ article before. It uses a nice algorithm from Knuth which rotates the elements in situ, leaving them in the original order at the end. Therefore, it meets your no external memory requirement. It also works for BidirectionalIterators. It doesn’t meet your requirement of looking like next_permutation(). However, I think this is a win – I don’t like stateful APIs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 87k
  • Answers 87k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I guess I would simply build such a page control.… May 11, 2026 at 5:31 pm
  • Editorial Team
    Editorial Team added an answer You can code a custom writer method: STATUS_VALUES = {… May 11, 2026 at 5:31 pm
  • Editorial Team
    Editorial Team added an answer Set the KeyPreview property on the Form to true. That… May 11, 2026 at 5:31 pm

Related Questions

Map type from STL have next type: std::map< Key, Data, Compare, Alloc > As
std::sort(range(c)); as opposed to std::sort(c.begin(), c.end(); Do you expect the next standard to provide
I have next code: #include <iostream> #include <algorithm> #include <map> #include <iterator> //namespace std
std::string provides const char* c_str ( ) const which: Get C string equivalent Generates

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.