- 相關推薦
C++中可以接受任意多個參數(shù)的函數(shù)定義方法
能夠接受任意多個參數(shù)的函數(shù),可以利用重載來實現(xiàn)。這種函數(shù)的執(zhí)行過程類似于遞歸調用,所以必須要有遞歸終止條件。本文特意為大家收集整理了C++中可以接受任意多個參數(shù)的函數(shù)定義方法,希望大家喜歡!
#include <iostream>
#include <bitset>
void print() {} // 遞歸終止條件。這是必需的。
template<typename Type, typename... Types>
void print(const Type& arg, const Types&... args)
{
std::cout << arg << std::endl;
print(args...);
}
int main()
{
print(1, 3.1415, "Hello, world!", 1.618, true, std::bitset<16>(377), 40);
return 0;
}
執(zhí)行后的結果如下:
1
3.1415
Hello, world!
1.618
1
0000000101111001
40
【C++中可以接受任意多個參數(shù)的函數(shù)定義方法】相關文章:
c和c++中實現(xiàn)函數(shù)回調的方法01-18
C++調用C函數(shù)的方法05-21
C語言程序中函數(shù)的定義05-28
php中數(shù)組的定義方法08-12