int add(int a,int b=2,int c =4);
- 1
其中有两个规定
1.如果某个参数是默认参数,那么它后面的参数必须是默认参数
2.默认参数可以放在声明、或者定义中,但是只能二者选一
以上是网上看的一个帖子:但是自己ubuntu18.04,g++编译的时候,必须把默认形参放在声明中,
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <forward_list>
#include <stack>
#include <vector>
#include <forward_list>
#include <deque>
#include <array>
#include <list>
#include <algorithm>
using namespace std;
void biggies(vector<string> & words,
vector<string>::size_type sz,ostream & os=cout , //;默认形参
char c = ' ' ); //默认形参
int main()
{
vector<string> vstr{"1","2","33","444","555","6666","77777","88888","89999","00000"};
biggies(vstr,100);
return 0;
}
void biggies(vector<string> &words,vector<string>::size_type sz,ostream &os,char c)
{
for_each(words.begin(),words.end(),[&os,c](const string &s){os << s << c;});
}