# Is there a function behaving the same as next\_permutation does in C++?

**URL:** https://discourse.julialang.org/t/is-there-a-function-behaving-the-same-as-next-permutation-does-in-c/63451
**Category:** General Usage
**Tags:** question
**Created:** [June 23, 2021, 3:39pm UTC](https://discourse.julialang.org/t/is-there-a-function-behaving-the-same-as-next-permutation-does-in-c/63451 "2021-06-23T15:39:46Z")
**Posts on this page:** 1
**Showing post:** 8

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [June 24, 2021, 5:47am UTC](https://discourse.julialang.org/t/is-there-a-function-behaving-the-same-as-next-permutation-does-in-c/63451/8 "2021-06-24T05:47:18Z")

</div>

A “next” permutation only makes sense when you have a clear definition of what “next” is supposed to mean. You seem to want it to mean “the next permutation of a given vector in a lexicographic ordering of all permutations of that vector”, which I don’t think is part of the Combinatorics.jl API by itself since that package deals with mathematical permutations in the form of vectors (i.e., lists of indices describing a permutation).

> [@AquaIndigo](#):
>
> `nthperm(nthperm([1, 2, 2], 2), 2)` will stay at `[1, 2, 2]` , which is not what I need.

If you expected `[2,1,2]` instead, you’ve hit an off-by-one error in your mental model, since julia uses 1-based indexing. The first and second permutation is the same and you didn’t specify whether you need unique permutations or not:

```julia
julia> permutations([1,2,2]) |> collect
6-element Vector{Vector{Int64}}:       
 [1, 2, 2]                             
 [1, 2, 2]                             
 [2, 1, 2]                             
 [2, 2, 1]                             
 [2, 1, 2]                             
 [2, 2, 1]                             

```

If that’s not what you expected, what do you want the result to be?

---

_[View the full topic](https://discourse.julialang.org/t/is-there-a-function-behaving-the-same-as-next-permutation-does-in-c/63451)._
