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

/ 4,885评论 / 16990阅读 / 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. Michaelpiepe说道:

    canadian pharmacy 24 com: canadian pharmacy checker – canadian valley pharmacy

  2. DavidWaype说道:

    canadian pharmacy 365: canadian world pharmacy – canadian pharmacy meds reviews

  3. EdwardKib说道:

    http://indiapharmast.com/# top 10 pharmacies in india

  4. youtube to mp3说道:

    Usually I do not read article on blogs however I would like to say that this writeup very compelled me to take a look at and do it Your writing style has been amazed me Thank you very nice article

  5. DavidWaype说道:

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

  6. Michaelpiepe说道:

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

  7. DavidWaype说道:

    canadian online pharmacy reviews: canadian pharmacy store – trusted canadian pharmacy

  8. Charlestaf说道:

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

  9. Charlestaf说道:

    canadian pharmacy no scripts canadian discount pharmacy legal canadian pharmacy online

  10. DavidWaype说道:

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

  11. Charlestaf说道:

    onlinepharmaciescanada com prescription drugs canada buy online onlinepharmaciescanada com

  12. EdwardKib说道:

    http://indiapharmast.com/# reputable indian pharmacies

  13. DavidWaype说道:

    best online pharmacies in mexico: mexico pharmacies prescription drugs – mexican rx online

  14. DavidWaype说道:

    trusted canadian pharmacy: best canadian online pharmacy reviews – canadian pharmacy world reviews

  15. Michaelpiepe说道:

    canadian pharmacy ratings: pharmacy com canada – canadian neighbor pharmacy

  16. EdwardKib说道:

    http://indiapharmast.com/# pharmacy website india

  17. You’ve made this complex topic easy to understand.구글 상위노출 백링크

  18. DavidWaype说道:

    canadian pharmacy world: vipps canadian pharmacy – canadian pharmacy prices

  19. Michaelpiepe说道:

    canadian 24 hour pharmacy: legitimate canadian pharmacies – online canadian pharmacy

  20. EdwardKib说道:

    https://canadapharmast.online/# certified canadian international pharmacy

  21. owner financing说道:

    It’s difficult to find experienced people in this particular topic, however, you seem like you know what you’re talking about! Thanks

  22. DavidWaype说道:

    buy prescription drugs from india: reputable indian pharmacies – best india pharmacy

  23. Charlestaf说道:

    canadadrugpharmacy com canadian pharmacy meds canadianpharmacy com

  24. Michaelpiepe说道:

    buy medicines online in india: indian pharmacy – best india pharmacy

  25. DavidWaype说道:

    india online pharmacy: Online medicine home delivery – indian pharmacy online

  26. DavidWaype说道:

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

  27. DavidWaype说道:

    pharmacy website india: Online medicine home delivery – indianpharmacy com

  28. Charlestaf说道:

    best online pharmacies in mexico mexico drug stores pharmacies buying prescription drugs in mexico online

  29. Charlestaf说道:

    Online medicine order reputable indian online pharmacy top 10 pharmacies in india

  30. EdwardKib说道:

    https://indiapharmast.com/# buy prescription drugs from india

  31. DavidWaype说道:

    canadian pharmacy king reviews: canadapharmacyonline – canadapharmacyonline

  32. Charlestaf说道:

    canada drug pharmacy canadian pharmacy uk delivery canadian pharmacy cheap

  33. Michaelpiepe说道:

    canadian discount pharmacy: canada drugs – canadian online drugstore

  34. DavidWaype说道:

    cheapest online pharmacy india: online pharmacy india – mail order pharmacy india

  35. EdwardKib说道:

    http://canadapharmast.com/# canadian pharmacy 24h com

  36. DavidWaype说道:

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

  37. Michaelpiepe说道:

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

  38. EdwardKib说道:

    https://foruspharma.com/# mexico drug stores pharmacies

  39. DavidWaype说道:

    indian pharmacy paypal: indian pharmacy – best india pharmacy

  40. Charlestaf说道:

    mexican pharmacy medicine in mexico pharmacies mexico drug stores pharmacies

  41. Michaelpiepe说道:

    canadian pharmacy in canada: best online canadian pharmacy – canada cloud pharmacy

  42. EdwardKib说道:

    http://indiapharmast.com/# best online pharmacy india

  43. Michaelpiepe说道:

    online shopping pharmacy india: mail order pharmacy india – india pharmacy mail order

  44. DavidWaype说道:

    buying from online mexican pharmacy: mexican border pharmacies shipping to usa – mexican online pharmacies prescription drugs

  45. DavidWaype说道:

    Online medicine home delivery: indianpharmacy com – top 10 pharmacies in india

  46. Charlestaf说道:

    my canadian pharmacy reviews reputable canadian online pharmacy ed drugs online from canada

  47. Charlestaf说道:

    Online medicine order indian pharmacy paypal online pharmacy india

  48. DavidWaype说道:

    mexico pharmacy: mexico pharmacy – п»їbest mexican online pharmacies

  49. youtube to mp3说道:

    I loved as much as you will receive carried out right here The sketch is attractive your authored material stylish nonetheless you command get got an impatience over that you wish be delivering the following unwell unquestionably come more formerly again since exactly the same nearly a lot often inside case you shield this hike

发表回复

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