- 相關推薦
C++中可以接受任意多個參數的函數定義方法
能夠接受任意多個參數的函數,可以利用重載來實現。這種函數的執行過程類似于遞歸調用,所以必須要有遞歸終止條件。本文特意為大家收集整理了C++中可以接受任意多個參數的函數定義方法,希望大家喜歡!
#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;
}
執行后的結果如下:
1
3.1415
Hello, world!
1.618
1
0000000101111001
40
【C++中可以接受任意多個參數的函數定義方法】相關文章:
C++調用C函數的方法05-21
c和c++中實現函數回調的方法08-30
Java程序調用C/C++語言函數的方法07-31
C++函數考點歸納09-30
java中定義常量的方法及介紹08-27
C語言函數的定義07-13
C++如何調用matlab函數06-29
Excel中COUNTIF函數的使用方法08-21
excel中rank函數使用方法06-07