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

/ 4,786评论 / 16809阅读 / 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. newsreality说道:

    What i do not understood is in truth how you are not actually a lot more smartlyliked than you may be now You are very intelligent You realize therefore significantly in the case of this topic produced me individually imagine it from numerous numerous angles Its like men and women dont seem to be fascinated until it is one thing to do with Woman gaga Your own stuffs nice All the time care for it up

  2. NelsonSaIlk说道:

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

  3. DominicHOr说道:

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

  4. forbesblogs说道:

    I have read some excellent stuff here Definitely value bookmarking for revisiting I wonder how much effort you put to make the sort of excellent informative website

  5. businesstrick说道:

    I was suggested this web site by my cousin Im not sure whether this post is written by him as no one else know such detailed about my trouble You are incredible Thanks

  6. Waynetar说道:

    mexican mail order pharmacies: medicine in mexico pharmacies – mexican rx online

  7. Arnoldscava说道:

    mexico pharmacies prescription drugs reputable mexican pharmacies online buying from online mexican pharmacy

  8. NelsonSaIlk说道:

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

  9. DominicHOr说道:

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

  10. Waynetar说道:

    п»їbest mexican online pharmacies: mexican mail order pharmacies – mexican online pharmacies prescription drugs

  11. Tonic Greens说道:

    Pretty nice post. I simply stumbled upon your weblog and wished to mention that I have truly loved surfing around your weblog posts. In any case I’ll be subscribing on your rss feed and I am hoping you write once more soon!

  12. Arnoldscava说道:

    mexican online pharmacies prescription drugs mexico pharmacy mexico drug stores pharmacies

  13. Arnoldscava说道:

    purple pharmacy mexico price list buying from online mexican pharmacy mexican mail order pharmacies

  14. NelsonSaIlk说道:

    buying prescription drugs in mexico: medication from mexico pharmacy – buying from online mexican pharmacy

  15. Waynetar说道:

    mexican drugstore online: mexican rx online – buying from online mexican pharmacy

  16. DominicHOr说道:

    mexico pharmacies prescription drugs: mexican rx online – mexican border pharmacies shipping to usa

  17. DominicHOr说道:

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

  18. Arnoldscava说道:

    mexico pharmacies prescription drugs mexico pharmacy mexico pharmacies prescription drugs

  19. NelsonSaIlk说道:

    pharmacies in mexico that ship to usa: pharmacies in mexico that ship to usa – medicine in mexico pharmacies

  20. DominicHOr说道:

    medicine in mexico pharmacies: pharmacies in mexico that ship to usa – purple pharmacy mexico price list

  21. Waynetar说道:

    mexican border pharmacies shipping to usa: п»їbest mexican online pharmacies – best online pharmacies in mexico

  22. Arnoldscava说道:

    mexican pharmacy п»їbest mexican online pharmacies mexican rx online

  23. DominicHOr说道:

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

  24. NelsonSaIlk说道:

    medication from mexico pharmacy: mexican online pharmacies prescription drugs – mexico drug stores pharmacies

  25. Waynetar说道:

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

  26. Waynetar说道:

    pharmacies in mexico that ship to usa: medicine in mexico pharmacies – mexico drug stores pharmacies

  27. Arnoldscava说道:

    medication from mexico pharmacy best online pharmacies in mexico mexico pharmacy

  28. Arnoldscava说道:

    reputable mexican pharmacies online mexican pharmaceuticals online mexican online pharmacies prescription drugs

  29. DominicHOr说道:

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

  30. NelsonSaIlk说道:

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

  31. Waynetar说道:

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

  32. DominicHOr说道:

    medicine in mexico pharmacies: buying from online mexican pharmacy – mexico drug stores pharmacies

  33. Arnoldscava说道:

    buying from online mexican pharmacy medication from mexico pharmacy mexican border pharmacies shipping to usa

  34. NelsonSaIlk说道:

    medicine in mexico pharmacies: buying prescription drugs in mexico online – medication from mexico pharmacy

  35. DominicHOr说道:

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

  36. NelsonSaIlk说道:

    mexico drug stores pharmacies: pharmacies in mexico that ship to usa – mexico pharmacies prescription drugs

  37. It’s hard to find knowledgeable people on this topic, but you sound like you know what you’re talking about! Thanks

  38. Arnoldscava说道:

    mexico drug stores pharmacies medication from mexico pharmacy mexican pharmaceuticals online

  39. DominicHOr说道:

    mexican rx online: mexican mail order pharmacies – п»їbest mexican online pharmacies

  40. Waynetar说道:

    mexican rx online: mexican rx online – mexican pharmaceuticals online

  41. Waynetar说道:

    mexican mail order pharmacies: buying from online mexican pharmacy – best online pharmacies in mexico

  42. Arnoldscava说道:

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

  43. Arnoldscava说道:

    mexico pharmacies prescription drugs mexico drug stores pharmacies mexican drugstore online

  44. DominicHOr说道:

    mexican pharmaceuticals online: buying from online mexican pharmacy – п»їbest mexican online pharmacies

  45. DominicHOr说道:

    purple pharmacy mexico price list: mexican pharmaceuticals online – mexican pharmaceuticals online

  46. Arnoldscava说道:

    medicine in mexico pharmacies buying prescription drugs in mexico online mexican drugstore online

  47. Waynetar说道:

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

  48. NelsonSaIlk说道:

    medicine in mexico pharmacies: mexican rx online – mexican mail order pharmacies

发表回复

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