Permutation of a vector

This is a discussion on Permutation of a vector within the C++ Programming forums, part of the General Programming Boards category; I was hoping for a little help on this subject. I am trying to find all the permutations of a ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    9

    Permutation of a vector

    I was hoping for a little help on this subject. I am trying to find all the permutations of a vector. Ive done a bit of research and could only find a recursive solution. Recursion still kind of confuses me, so I was wondering if anyone could help with a non recursive version? Heres what I have so far:

    Code:
    void perm(vector<int>v, int num, int curNum, int posNum, int &output)
    {
    	v.insert(v.begin()+posNum, curNum);//puts the current number into the desired position
    	
    	if(curNum == num)//stop case
    	{
    		print(v, num, output);//print function
    		return;
    	}
    
    	for(int i=0; i <= curNum; i++)
    	{
    		perm(v, num, curNum+1, i, output);//recursive call
    	}
    }

  2. #2
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    What exactly do you mean by finding all permutations of a vector?
    Explain

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    this is the perfect chance to learn about recursion.
    make this modification:
    Code:
    void perm(vector<int>v, int num, int curNum, int posNum, int &output, int level)
    {
    	v.insert(v.begin()+posNum, curNum);//puts the current number into the desired position
    
    	if(curNum == num)//stop case
    	{
    		print(v, num, output, level);//print function
    		return;
    	}
    
    	for(int i=0; i <= curNum; i++)
    	{
    		perm(v, num, curNum+1, i, output, level++);//recursive call
    	}
    }
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    Code:
    #include <algorithm>
    #include <vector>
    
    ...
    
    std::vector<int> intVect;
    
    ...
    
    std::sort(intVect.begin(),intVect.end());
    print(...);      // Call your print function
    while( std::next_permutation(intVect.begin(), intVect.end()) )
        print(...);  // Call your print function
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    9
    Thanks for the help everyone. This has helped me greatly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. syntax help?
    By scoobygoo in forum C++ Programming
    Replies: 1
    Last Post: 08-07-2007, 10:38 AM
  3. Vector class
    By Desolation in forum C++ Programming
    Replies: 2
    Last Post: 05-12-2007, 05:44 PM
  4. Need some help/advise for Public/Private classes
    By nirali35 in forum C++ Programming
    Replies: 8
    Last Post: 09-23-2006, 12:34 PM
  5. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 12:26 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21