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

/ 325评论 / 1996阅读 / 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. Terrydex说道:

    buy medications online without prescription: cheapest and fast – best online pharmacy that does not require a prescription in india

  2. StevenDouri说道:

    http://cheapestandfast.com/# no prescription medicines

  3. CharlesOveno说道:

    best online pharmacy india buy medicines online in india world pharmacy india

  4. StevenDouri说道:

    https://cheapestindia.com/# Online medicine home delivery

  5. Jimmieexake说道:

    https://cheapestmexico.shop/# medication from mexico pharmacy

  6. StevenDouri说道:

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

  7. CharlesOveno说道:

    canadian pharmacy coupon code 36 and 6 pharmacy no prescription required pharmacy

  8. ThomasTam说道:

    zestril tablet: zestril 10 mg online – price of lisinopril

  9. Marvinmef说道:

    cost cheap clomid pills where buy generic clomid buy generic clomid

  10. ThomasTam说道:

    cheapest price for lisinopril: cost of lisinopril 5 mg – lisinopril 30 mg tablet

  11. RobertPet说道:

    https://clomiphene.shop/# can you get clomid online

  12. Robertguelm说道:

    can i purchase clomid without insurance: where buy generic clomid tablets – can i purchase cheap clomid no prescription

  13. JamesPouff说道:

    http://clomiphene.shop/# can i purchase cheap clomid

  14. Marvinmef说道:

    buy neurontin 100 mg neurontin 1000 mg generic neurontin cost

  15. ThomasTam说道:

    prices for lisinopril: lisinopril 90 pills cost – lisinopril 30 mg price

  16. JamesPouff说道:

    https://propeciaf.online/# cheap propecia online

  17. Marvinmef说道:

    buy cytotec online Cytotec 200mcg price Misoprostol 200 mg buy online

  18. ThomasTam说道:

    can you buy cheap clomid prices: how can i get cheap clomid without insurance – where buy cheap clomid no prescription

  19. JamesPouff说道:

    https://propeciaf.online/# buying cheap propecia without prescription

  20. Marvinmef说道:

    get generic propecia tablets buy cheap propecia without insurance buy propecia price

  21. ThomasTam说道:

    how can i get generic clomid for sale: how to get cheap clomid without a prescription – can you buy generic clomid pills

  22. JamesPouff说道:

    http://lisinopril.club/# generic lisinopril 5 mg

  23. Marvinmef说道:

    where to get clomid pills can i order generic clomid without dr prescription can you get cheap clomid no prescription

  24. ThomasTam说道:

    zestril 30 mg: lisinopril 30 mg price – average cost of lisinopril

发表回复

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