Thread: next_permutation on 2D array

  1. #1
    Registered User
    Join Date
    Nov 2005

    Question next_permutation on 2D array

    If I want all permutations of 1D array I have:
    #include <iostream>
    #include <algorithm>
    using namespace std;
    main() {
    	int arr[4]={1,2,3,4};
    	for (int i=0; i<=23; i++) { 
    		next_permutation(arr, arr+4);
    		for (int j=0; j<=3; j++) {
    			cout << arr[j] << ", ";
    	cout << endl;}
    return 0;}
    But if I have arr={{1,1,1}, {2,2,2}, {3,3,3}, {4,4,4}}, I don't know what should I put between brackets of next_permutation(), I tried next_permutation(arr[3], (arr+3)[3]), it does compile, but it does not permutate.
    I'd like a solution to be:
    1,1,1,2,2,2,3,3,3,4,4,4, (the order is not important)

  2. #2
    Registered User
    Join Date
    Jan 2006
    I think it's not possible without some extra work.
    next_permutation() works with a sequence and corresponding iterator. With 2D array you should have some 2D iterator which works with rows of that array. When swapping two rows, function should be able to get whole row at once with dereferencing using * operator. That is not possible with 2D array and ordinary int pointer, i.e. it is, but it does not work what you expect.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Can you post the full code with the 2D array and your attempt?

    It looks like you don't need the values in each row permuted, right? If that is the case you should be able to do this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  2. Help with mallocing a 2d array please?
    By Gatt9 in forum C Programming
    Replies: 5
    Last Post: 10-10-2008, 03:45 AM
  3. 2D array pointer?
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 04-23-2006, 08:16 AM
  4. Read file in 2D array
    By Chook in forum C Programming
    Replies: 1
    Last Post: 05-08-2005, 12:39 PM
  5. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM