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

/ 4,888评论 / 17000阅读 / 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. NelsonSaIlk说道:

    mexican drugstore online: buying prescription drugs in mexico – buying prescription drugs in mexico online

  2. DominicHOr说道:

    pharmacies in mexico that ship to usa: mexican pharmaceuticals online – buying prescription drugs in mexico online

  3. NelsonSaIlk说道:

    mexican border pharmacies shipping to usa: mexican pharmaceuticals online – best online pharmacies in mexico

  4. Waynetar说道:

    buying prescription drugs in mexico online: medicine in mexico pharmacies – mexican mail order pharmacies

  5. Arnoldscava说道:

    buying prescription drugs in mexico online buying prescription drugs in mexico п»їbest mexican online pharmacies

  6. DominicHOr说道:

    buying from online mexican pharmacy: purple pharmacy mexico price list – medication from mexico pharmacy

  7. NelsonSaIlk说道:

    п»їbest mexican online pharmacies: mexican online pharmacies prescription drugs – mexico drug stores pharmacies

  8. Waynetar说道:

    mexico pharmacies prescription drugs: medicine in mexico pharmacies – buying prescription drugs in mexico

  9. Arnoldscava说道:

    medication from mexico pharmacy mexican rx online mexican mail order pharmacies

  10. Arnoldscava说道:

    mexican mail order pharmacies mexican mail order pharmacies buying prescription drugs in mexico

  11. Waynetar说道:

    medicine in mexico pharmacies: mexican mail order pharmacies – buying prescription drugs in mexico online

  12. DominicHOr说道:

    best online pharmacies in mexico: medicine in mexico pharmacies – purple pharmacy mexico price list

  13. DominicHOr说道:

    best online pharmacies in mexico: mexico drug stores pharmacies – mexico pharmacies prescription drugs

  14. Arnoldscava说道:

    buying prescription drugs in mexico online mexican border pharmacies shipping to usa mexican border pharmacies shipping to usa

  15. Waynetar说道:

    mexican rx online: mexican mail order pharmacies – mexican online pharmacies prescription drugs

  16. NelsonSaIlk说道:

    mexico drug stores pharmacies: mexican drugstore online – buying from online mexican pharmacy

  17. DominicHOr说道:

    medication from mexico pharmacy: best online pharmacies in mexico – medication from mexico pharmacy

  18. NelsonSaIlk说道:

    pharmacies in mexico that ship to usa: п»їbest mexican online pharmacies – buying prescription drugs in mexico

  19. NelsonSaIlk说道:

    mexican mail order pharmacies: buying prescription drugs in mexico online – mexican rx online

  20. Also, you can have the best rates for money exchange.

  21. Arnoldscava说道:

    mexican drugstore online pharmacies in mexico that ship to usa best online pharmacies in mexico

  22. Waynetar说道:

    buying prescription drugs in mexico online: mexican pharmaceuticals online – mexico pharmacies prescription drugs

  23. DominicHOr说道:

    mexican online pharmacies prescription drugs: buying from online mexican pharmacy – pharmacies in mexico that ship to usa

  24. NelsonSaIlk说道:

    buying prescription drugs in mexico: pharmacies in mexico that ship to usa – medication from mexico pharmacy

  25. Waynetar说道:

    mexican pharmaceuticals online: mexican drugstore online – mexico drug stores pharmacies

  26. Arnoldscava说道:

    medication from mexico pharmacy mexican pharmaceuticals online reputable mexican pharmacies online

  27. Arnoldscava说道:

    purple pharmacy mexico price list п»їbest mexican online pharmacies mexican online pharmacies prescription drugs

  28. DominicHOr说道:

    mexican pharmaceuticals online: medication from mexico pharmacy – best online pharmacies in mexico

  29. Waynetar说道:

    mexican drugstore online: pharmacies in mexico that ship to usa – mexican rx online

  30. DominicHOr说道:

    mexico drug stores pharmacies: mexican mail order pharmacies – mexico pharmacies prescription drugs

  31. Arnoldscava说道:

    reputable mexican pharmacies online mexican border pharmacies shipping to usa mexican pharmacy

  32. Waynetar说道:

    п»їbest mexican online pharmacies: best online pharmacies in mexico – mexico drug stores pharmacies

  33. NelsonSaIlk说道:

    mexican rx online: mexico pharmacies prescription drugs – purple pharmacy mexico price list

  34. DominicHOr说道:

    mexican mail order pharmacies: mexico drug stores pharmacies – mexican drugstore online

  35. NelsonSaIlk说道:

    mexico drug stores pharmacies: medication from mexico pharmacy – reputable mexican pharmacies online

  36. NelsonSaIlk说道:

    medicine in mexico pharmacies: buying prescription drugs in mexico online – mexican rx online

  37. Arnoldscava说道:

    mexican mail order pharmacies mexico pharmacies prescription drugs mexico pharmacies prescription drugs

  38. Waynetar说道:

    mexico drug stores pharmacies: medicine in mexico pharmacies – buying prescription drugs in mexico online

  39. DominicHOr说道:

    medication from mexico pharmacy: medicine in mexico pharmacies – reputable mexican pharmacies online

  40. NelsonSaIlk说道:

    best online pharmacies in mexico: mexico drug stores pharmacies – best online pharmacies in mexico

  41. Waynetar说道:

    medication from mexico pharmacy: buying prescription drugs in mexico online – purple pharmacy mexico price list

  42. Arnoldscava说道:

    mexico pharmacies prescription drugs pharmacies in mexico that ship to usa reputable mexican pharmacies online

  43. DominicHOr说道:

    medicine in mexico pharmacies: buying prescription drugs in mexico online – mexican rx online

  44. Arnoldscava说道:

    mexico pharmacy medication from mexico pharmacy п»їbest mexican online pharmacies

  45. DominicHOr说道:

    best online pharmacies in mexico: mexican rx online – reputable mexican pharmacies online

  46. Arnoldscava说道:

    п»їbest mexican online pharmacies buying prescription drugs in mexico mexican pharmaceuticals online

  47. Waynetar说道:

    mexican drugstore online: mexico pharmacies prescription drugs – mexican pharmaceuticals online

  48. NelsonSaIlk说道:

    best online pharmacies in mexico: purple pharmacy mexico price list – best online pharmacies in mexico

  49. DominicHOr说道:

    mexican pharmaceuticals online: mexican rx online – mexico drug stores pharmacies

  50. NelsonSaIlk说道:

    best online pharmacies in mexico: mexican drugstore online – best online pharmacies in mexico

发表回复

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