I can't understand the behavior of my vector in this recursion function

This is a discussion on I can't understand the behavior of my vector in this recursion function within the C++ Programming forums, part of the General Programming Boards category; Hi I have a class where I define a Code: vector<char *> myVector to be a private variable. This variable ...

  1. #1
    nik
    nik is offline
    Registered User
    Join Date
    Nov 2010
    Posts
    44

    I can't understand the behavior of my vector in this recursion function

    Hi I have a class where I define a
    Code:
     vector<char *> myVector
    to be a private variable.

    This variable will store permutations of a string

    For example, if the string is "123" it will store "123","213","321" etc

    I then define a function that does the permutations, the function is the following:

    Code:
    void PermClass::findPerms(char *set, int begin, int end){
    
    	int i;
    	int range = end - begin;
    	if(range == 0) myVector.push_back(set);//(1)
    	else{
    
    		for(i=0;i<range;i++){
    			swap(set[begin],set[begin+i]);
    			findPerms(set,begin+1,end);
    			swap(set[begin],set[begin+i]);
    		}
    	}
    }
    I have tried some debugging and realized that the variable "set" has the next permutation at position (1).

    However, when the recursion ends, when I output the values of the vector by using an iterator like this:

    Code:
    void PermClass::showVector(){
    
    	int i;
    	vector<char *>::iterator it;
    	for(it = myVector.begin(); it!=myVector.end(); it++){
    		cout<<*it<<endl;
    	}
    	cin.get();
    
    }
    I get as an output

    Code:
    123
    123
    123
    123
    123
    123
    can someone explain me what's wrong? thanks in advance

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,753
    It looks like you are just storing pointers to the same string. Maybe you should use a vector<string> instead.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    nik
    nik is offline
    Registered User
    Join Date
    Nov 2010
    Posts
    44
    Quote Originally Posted by laserlight View Post
    It looks like you are just storing pointers to the same string. Maybe you should use a vector<string> instead.
    thanks a lot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strange behavior with array in function
    By pater in forum C Programming
    Replies: 4
    Last Post: 01-24-2012, 06:02 AM
  2. Bizarre behavior of the string::find() function
    By Programmer_P in forum C++ Programming
    Replies: 19
    Last Post: 03-09-2011, 10:36 AM
  3. Replies: 4
    Last Post: 02-20-2011, 05:29 PM
  4. I don't understand this for function.
    By Dorky King in forum C Programming
    Replies: 12
    Last Post: 06-12-2007, 03:18 PM
  5. Replies: 13
    Last Post: 08-24-2006, 12:22 AM

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