C++的缺省參數(shù)你了解嘛
一、缺省參數(shù)概念
缺省參數(shù)是聲明或定義函數(shù)時為函數(shù)的參數(shù)指定一個默認(rèn)值。在調(diào)用該函數(shù)時,如果沒有指定實(shí)參則采用該默認(rèn)值,否則使用指定的實(shí)參
#include<iostream>
using namespace std;
void TestFunc(int a = 0)//參數(shù)缺省值
{
cout << a << endl;
}
int main()
{
TestFunc();//沒有指定實(shí)參,使用缺省值
TestFunc(10);//指定實(shí)參,使用實(shí)參
return 0;
}
有什么用
比如在 C 語言中有個很苦惱的問題是寫棧時,不知道要開多大的空間,之前我們是如果棧為空就先開 4 塊空間,之后再以 2 倍走,如果我們明確知道要很大的空間,那么這樣就只能一點(diǎn)一點(diǎn)的接近這塊空間,就太 low 了。但如果我們使用缺省,明確知道不需要太大時就使用默認(rèn)的空間大小,明確知道要很大時再傳參
#include<iostream>
using namespace std;
namespace WD
{
struct Stack
{
int* a;
int size;
int capacity;
};
}
using namespace WD;
void StackInit(struct Stack* ps)
{
ps->a = NULL;
ps->capacity = 0;
ps->size = 0;
}
void StackPush(struct Stack* ps, int x)
{
if(ps->size == ps->capacity)
{
//ps->capacity *= 2;//err
ps->capacity == 0 ? 4 : ps->capacity * 2;//這里就必須寫一個三目
}
}
void StackInitCpp1(struct Stack* ps, int defaultCP)
{
ps->a = (int*)malloc(sizeof(int) * defaultCP);
ps->capacity = 0;
ps->size = defaultCP;
}
void StackInitCpp2(struct Stack* ps, int defaultCP = 4)//ok
{
ps->a = (int*)malloc(sizeof(int) * defaultCP);
ps->capacity = 0;
ps->size = defaultCP;
}
int main()
{
//假設(shè)明確知道這里至少需要100個數(shù)據(jù)到st1
struct Stack st1;
StackInitCpp1(&st1, 100);
//假設(shè)不知道st2里需要多少個數(shù)據(jù) ———— 希望開小點(diǎn)
struct Stack st2;
StackInitCpp2(&st1);//缺省
return 0;
}
二、缺省參數(shù)分類
全缺省參數(shù)
void TestFunc(int a = 10, int b = 20, int c = 30)
{
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "c = " << c << endl;
cout << endl;
}
int main()
{
//非常靈活,
TestFunc();
TestFunc(1);
TestFunc(1, 2);
TestFunc(1, 2, 3);
//TestFunc(1, , 3);//err,注意它沒辦法實(shí)現(xiàn)b不傳,只傳a和b,也就是說編譯器只能按照順序傳
return 0;
}
注意:
全缺省參數(shù)只支持順序傳參
半缺省參數(shù)
//void TestFunc(int a, int b = 10, /*int f, - err*/ int c = 20);//err,void TestFunc(int a, int b = 10, /*int f, int x = y, -> err*/ int c = 20){cout << "a = " << a << endl;cout << "b = " << b << endl;cout << "c = " << c << endl;cout << endl;}int main(){//TestFunc();//err,至少得傳一個,這是根據(jù)形參有幾個非半缺省參數(shù)確定的TestFunc(1);TestFunc(1, 2);TestFunc(1, 2, 3);return 0;}//void TestFunc(int a, int b = 10, /*int f, - err*/ int c = 20);//err,
void TestFunc(int a, int b = 10, /*int f, int x = y, -> err*/ int c = 20)
{
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "c = " << c << endl;
cout << endl;
}
int main()
{
//TestFunc();//err,至少得傳一個,這是根據(jù)形參有幾個非半缺省參數(shù)確定的
TestFunc(1);
TestFunc(1, 2);
TestFunc(1, 2, 3);
return 0;
}
//a.hvoid TestFunc(int a = 10);//a.cppvoid TestFunc(int a = 20){}注意:
- 半缺省參數(shù)必須從右往左依次來給出,且不能間隔著給
- 缺省參數(shù)不能在函數(shù)聲明和定義中同時出現(xiàn)
- 缺省值必須是常量或者全局變量
- C 語言不支持缺省
總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
C語言編程中從密碼文件獲取數(shù)據(jù)的函數(shù)總結(jié)
這篇文章主要介紹了C語言編程中從密碼文件獲取數(shù)據(jù)的函數(shù)總結(jié),包括getpw()函數(shù)和getpwnam()函數(shù)以及getpwuid()函數(shù),需要的朋友可以參考下2015-08-08
一文帶你快速了解C/C++標(biāo)準(zhǔn)庫中的ptrdiff_t
ptrdiff_t是C/C++標(biāo)準(zhǔn)庫中定義的一個與機(jī)器相關(guān)的數(shù)據(jù)類型,ptrdiff_t類型變量通常用來保存兩個指針減法操作的結(jié)果,下面這篇文章主要給大家介紹了關(guān)于C/C++標(biāo)準(zhǔn)庫中ptrdiff_t的相關(guān)資料,需要的朋友可以參考下2022-11-11
VS2019使用Windows桌面應(yīng)用程序模塊創(chuàng)建Win32窗口
這篇文章主要介紹了VS2019使用Windows桌面應(yīng)用程序模塊創(chuàng)建Win32窗口,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
C++標(biāo)準(zhǔn)模板庫函數(shù)sort的那些事兒
sort函數(shù)是標(biāo)準(zhǔn)模板庫的函數(shù),已知開始和結(jié)束的地址即可進(jìn)行排序,可以用于比較任何容器(必須滿足隨機(jī)迭代器),任何元素,任何條件,執(zhí)行速度一般比qsort要快2013-09-09
C C++ 題解LeetCode2360圖中的最長環(huán)示例
這篇文章主要為大家介紹了C C++ 題解LeetCode2360圖中的最長環(huán)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
C語言實(shí)現(xiàn)簡易停車場管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)簡易停車場管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03

