string permutation using recursion

i'm learning about recursion, but when i debug this code, it produce segmentation fault (core dump), what is wrong with the code?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
  #include <bits/stdc++.h> 
using namespace std; 
  
  
// Function to print permutations of string  
// This function takes three parameters:  
// 1. String  
// 2. vector to store string permutation
// 3. dummy vector to store each permutation
vector<vector<int>> permute(vector<int>nums,vector<vector<int>>k,vector<int>li)  
{  
if(nums.empty()){
k.push_back(li);
}else{
  for(int i=0;i<nums.size();i++){
   int chos=nums[i];
   li.push_back(chos);
   nums.erase(nums.begin()+i);
   permute(nums,k,li);
   nums.insert(nums.begin()+i,chos);
   li.erase(li.end());


  }

}
}  
  
// Driver Code  
int main()  
{  
   vector<int>permut={1,2,3};
   vector<vector<int>>k; 
   vector<int>li;  
   permute(permut,k,li);  
    return 0;  
}  
  
Compile with warnings:
g++ -Wall -g -D DEBUG -std=c++14 -c foo.cxx
foo.cxx: In function ‘std::vector<std::vector<int> > permute(std::vector<int>, std::vector<std::vector<int> >, std::vector<int>)’:
foo.cxx:15:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i=0;i<nums.size();i++){
               ~^~~~~~~~~~~~
foo.cxx:27:1: warning: no return statement in function returning non-void [-Wreturn-type]
 }
 ^
g++ -L. -g foo.o -lm -o foo.exe

A quick check shows that permute() is supposed to return vector<vector<int>> but doesn't.

Also, vector<>::end(), by definition, doesn't refer to a valid value, so line 23 wrong. Use li.pop_back() instead.

That gets the code to run but it still doesn't do any thing. Here is a version that prints the result returned by permute() and also prints the nums and li args of each call to permute. This should help you debug the function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <bits/stdc++.h>
using namespace std;


template <class T>
ostream & operator<< (ostream & os, const vector<T> vec) {
    for (auto &val : vec) {
        cout << val << ' ';
    }
    return os;
}

// Function to print permutations of string
// This function takes three parameters:
// 1. String
// 2. vector to store string permutation
// 3. dummy vector to store each permutation
vector<vector<int>> permute(vector<int>nums,vector<vector<int>>k,vector<int>li)
{
    cout << "permute(" << nums << ", ...., " << li << ")\n";

if(nums.empty()){
k.push_back(li);
}else{
  for(int i=0;i<nums.size();i++){
   int chos=nums[i];
   li.push_back(chos);
   nums.erase(nums.begin()+i);
   permute(nums,k,li);
   nums.insert(nums.begin()+i,chos);
   li.pop_back();               // dmh


  }

}
 return k;
}

// Driver Code
int main()
{
   vector<int>permut={1,2,3};
   vector<vector<int>>k;
   vector<int>li;
   k = permute(permut,k,li);

   for (auto &vec : k) {
       cout << vec << '\n';
   }
    return 0;
}

Topic archived. No new replies allowed.