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

  1. #1
    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
    28,413
    It looks like you are just storing pointers to the same string. Maybe you should use a vector<string> instead.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    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, 07:02 AM
  2. Bizarre behavior of the string::find() function
    By Programmer_P in forum C++ Programming
    Replies: 19
    Last Post: 03-09-2011, 11:36 AM
  3. Replies: 4
    Last Post: 02-20-2011, 06: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