KiraDeus
KiraDeus

Reputation: 39

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

Upvotes: 1

Views: 594

Answers (2)

πάντα ῥεῖ
πάντα ῥεῖ

Reputation: 1

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.

Upvotes: 2

Sam Varshavchik
Sam Varshavchik

Reputation: 118445

A default value can only be specified for the last set of parameters to a function:

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

Here, a default value is provided for the 2nd of three parameters. If a default value is provided for the 2nd parameter, one must be provided for the third one as well. A parameter with a default value cannot be followed by a parameter without a default value.

However, in this case it looks like the default value is not needed at all, so just remove it.

Upvotes: 0

Related Questions