KiraDeus KiraDeus - 2 months ago 7
C++ Question

What should the default argument(if any) be while passing a vector to a function?

I have written this program :

#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
using namespace std;

void permute(char a[],int i=0, vector<string> &vs){
if(a[i]=='\0'){
//cout<<a<<endl;
string s(a);
vs.push_back(s);
return;
}

for(int j=i;a[j]!='\0';j++){
swap(a[i],a[j]);
permute(a,i+1,vs);
swap(a[i],a[j]);
}
}


int main()
{
char a[25] ;
cin>>a;
vector<string> vs;

permute (a,0,vs) ;

sort(vs.begin(),vs.end());

for(int j=0;j<vs.size();j++)
{
cout<<vs[j]<<endl;
}

return 0;
}


The complier says "default argument missing for parameter 3 of void permute()
I do not know how to give a default value to the vector vs that I am passing.
Please help

Answer

As soon you have given a default value for one parameter you have to give one for all of the following.

So just change the order of your parameters:

 void permute(char a[], vector<string> &vs,int i=0){

For a vector<string> & parameter, you can't really pass a reasonable default value but a global reference or such.

Comments