Click here to Skip to main content
15,910,009 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
There is an problem about how to iterate all the situations of sorting.

Suppose there are 4 numbers:1,2,3,4, and how to display all the situations of sorting using C++ function , f(x) where x is the count of numbers.

example:
1234
1243
1324
1342
1423
1432
2134
2143
2314
2341
2413
2431
3124
3142
3214
3241
3412
3421
4123
4132
4213
4231
4312
4321
Posted

1 solution

The algorithm for this is already available in STL.
The function is called std::next_permutation and is available in the <algorithm> header.

Create a vector and push back the digits 1, 2, 3 and 4 into the vector.
Call next_permutation in a loop till it returns false with the vector::begin() and vector::end() methods as parameters.
 
Share this answer
 
Comments
virusx1984 28-Jul-11 10:27am    
thank you!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900