[c++模板] 用可变参数模板来实现 printf

/ 325评论 / 1999阅读 / 3点赞

模板

// 普通函数, 只能接受 int 类型参数,如果传入其他类型需要类型转换
int sum(int a, int b) {
    return (a + b);
}

double sum(double a, double b) {
    return (a + b);
}
// 模板
template<typename T>
T sum(T a, T b) {
    return (a + b);
}

// 编译会生成:
int sum(int a, int b) {
    return (a + b);
}
double sum(double a, double b) {
    return (a + b);
}

可变参数模板

int sum(int a, int b, int c) {
    return (a + b + c);
}

int sum(int a, int b, int c, int d) {
    return (a + b + c + d);
}
// ......
template<typename T>
T sum(T... args) {
    // 展开求和
}
template<typename T>
T mysum(T item) {
    return item;
}
template<typename T>
T mysum(T item, T... args) {
    return mysum(item) + mysum(args);
}
/// 入口
template<typename T>
T sum(T... args) {
    return mysum(args);
}
int sum(int arg0, int arg1, int arg2, int arg3, int arg4);
int mysum(int item, int arg1, int arg2, int arg3, int arg4);
int mysum(int item);
int mysum(int item, int arg2, int arg3, int arg4);
int mysum(int item, int arg3, int arg4);
int mysum(int item, int arg4);

不定类型数量

template<typename T1, typename T2, typename ...Args>
T1 mysum(T2 item, Args... args);

实现printf

template<typename T>
int printNum(const std::string& str, int index, T&& item) {
	auto doShift = true;
	while (index < str.size()) {
		if (index == (str.size() - 1) || str[index] != '%') {
			cout << str[index];
			++index;
		}
		else if(doShift) {
			doShift = false;
			++index;
			switch (str[index]) {
			case 's':
			case 'd':
				cout << item;
				break;
			}
			++index;
		}
		else {
			return index - 1;
		}
	}
	return index - 1;
}

template<typename T, typename... Args>
int printNum(const std::string& str, int index, T&& item, Args&&... args) {
	index = printNum(str, index, item);
	return printNum(str, index + 1, std::forward<Args>(args)...);
}

template<typename ...Args>
void myPrintNum(const std::string& str, Args&&... args) {
	int index = 0;
	while (index < str.size()) {
		if (str[index] != '%') {
			cout << str[index];
			++index;
		}
		else {
			break;
		}
	}
	index = printNum(str, index, std::forward<Args>(args)...);
	++index;
	while (index < str.size()) {
		cout << str[index];
		++index;
	}
}

int main() {

        // 调用,参数数量允许和字符串内数量不同,不会出错
	myPrintNum("output: %d, %d, %d, %d  ---", 123, 100.123, true);
	return 0;
}
  1. Elmersoalf说道:

    Cenforce 100mg tablets for sale Purchase Cenforce Online Cenforce 150 mg online

  2. Georgefrege说道:

    Kamagra 100mg price: kamagra.win – Kamagra Oral Jelly

  3. Charlesvox说道:

    http://viagras.online/# order viagra

  4. Elmersoalf说道:

    Generic Tadalafil 20mg price cialist.pro Tadalafil Tablet

  5. Jamesexite说道:

    buy Viagra over the counter: Buy Viagra online cheap – Generic Viagra for sale

  6. Charlesvox说道:

    http://cialist.pro/# Generic Tadalafil 20mg price

  7. Elmersoalf说道:

    sildenafil oral jelly 100mg kamagra kamagra Kamagra tablets

  8. Jamesexite说道:

    buy cialis pill: Cialis 20mg price in USA – Cialis without a doctor prescription

  9. Georgefrege说道:

    Cenforce 150 mg online: Buy Cenforce 100mg Online – order cenforce

  10. DavidBiolo说道:

    https://kamagra.win/# cheap kamagra

  11. Tylerreunk说道:

    http://viagras.online/# Cheap generic Viagra

  12. Elmersoalf说道:

    Levitra 20 mg for sale Cheap Levitra online Levitra online USA fast

  13. Charlesvox说道:

    https://levitrav.store/# Levitra 20 mg for sale

  14. Davidfrify说道:

    lisinopril 20mg discount buy lisinopril 20 mg online zestril brand

  15. WilliamRen说道:

    http://finasteride.store/# buy propecia without a prescription

  16. RandallGuesE说道:

    buy generic ciprofloxacin: ciprofloxacin order online – ciprofloxacin generic price

  17. Davidfrify说道:

    tamoxifen hot flashes tamoxifen therapy tamoxifen vs clomid

  18. RonaldSwobe说道:

    https://finasteride.store/# buy generic propecia no prescription

  19. WilliamRen说道:

    https://cytotec.club/# Abortion pills online

  20. RandallGuesE说道:

    zestril generic: lisinopril 20 mg tablet price – lisinopril 5 mg brand name

  21. Davidfrify说道:

    common side effects of tamoxifen liquid tamoxifen benefits of tamoxifen

  22. WilliamRen说道:

    http://ciprofloxacin.tech/# buy cipro cheap

  23. RandallGuesE说道:

    best generic lisinopril: cheapest lisinopril 10 mg – zestoretic 10 12.5

  24. Davidfrify说道:

    tamoxifen adverse effects where to buy nolvadex alternative to tamoxifen

  25. WilliamRen说道:

    http://ciprofloxacin.tech/# buy ciprofloxacin over the counter

  26. RonaldSwobe说道:

    https://nolvadex.life/# tamoxifen for men

  27. RandallGuesE说道:

    buy cytotec over the counter: buy cytotec in usa – buy cytotec over the counter

  28. Davidfrify说道:

    buy cytotec pills online cheap cytotec abortion pill buy misoprostol over the counter

  29. RandallGuesE说道:

    tamoxifen for sale: does tamoxifen cause bone loss – tamoxifen and bone density

  30. Davidfrify说道:

    aromatase inhibitor tamoxifen nolvadex gynecomastia tamoxifen adverse effects

  31. WilliamRen说道:

    https://finasteride.store/# buy cheap propecia prices

  32. RandallGuesE说道:

    lisinopril 10mg daily: lisinopril 2.5 tablet – lisinopril 2.5 mg price

  33. Davidfrify说道:

    buy generic ciprofloxacin ciprofloxacin order online buy cipro

  34. WilliamRen说道:

    http://ciprofloxacin.tech/# cipro 500mg best prices

  35. RandallGuesE说道:

    buy lisinopril online: 40 mg lisinopril for sale – lisinopril oral

  36. Davidfrify说道:

    antibiotics cipro cipro 500mg best prices buy cipro cheap

  37. RandallGuesE说道:

    buy cipro online canada: buy cipro – ciprofloxacin generic price

  38. RonaldSwobe说道:

    http://ciprofloxacin.tech/# buy cipro online

  39. Davidfrify说道:

    ciprofloxacin generic cipro for sale ciprofloxacin 500mg buy online

  40. WilliamRen说道:

    https://ciprofloxacin.tech/# antibiotics cipro

  41. RandallGuesE说道:

    cost of tamoxifen: nolvadex vs clomid – what is tamoxifen used for

  42. Davidfrify说道:

    tamoxifen reviews tamoxifen and grapefruit natural alternatives to tamoxifen

  43. WilliamRen说道:

    http://nolvadex.life/# aromatase inhibitor tamoxifen

  44. Davidfrify说道:

    get generic propecia prices buying generic propecia price buying generic propecia online

  45. RandallGuesE说道:

    get propecia without a prescription: generic propecia pill – cost of cheap propecia without dr prescription

  46. RonaldSwobe说道:

    https://finasteride.store/# order generic propecia without dr prescription

  47. Davidfrify说道:

    generic propecia no prescription order propecia pills propecia without prescription

  48. WilliamRen说道:

    https://ciprofloxacin.tech/# ciprofloxacin order online

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注