zoukankan      html  css  js  c++  java
  • 实现字符串拼接(可变参数的)

    因为工作需要,需要频繁用的字符串拼接,参数还不固定,所以写了下面的例子,算是给自己的记录

    #include "stdafx.h"
    #include <iostream>
    #include <string>
    #include <sstream> 
    
    using namespace std;
    
    template<class T>
    
    void addString(string& strItem, T arg)
    {
    	std::ostringstream oss;
    	std::string strValue = "";
    	oss << arg;
    	strValue = oss.str();
    	strItem += strValue;
    }
    
    template<class... Args>
    std::string stringOutput(Args... args) 
    {
    	std::string strRet;
    	int arr[] = { (addString(strRet, args), 0)... };
    	return strRet;
    }
    int main()
    {
    	string str = "qwe";
    	char buf[12] = { 0 };
    	memcpy(buf, str.c_str(), str.length());
    	std::cout << stringOutput("你好~~~", 1234, "OKk", 1.23546, buf, str);
    
        return 0;
    }
    

      

  • 相关阅读:
    MySQL-percona安装
    Oracle-19C PSU升级
    Oracle-内存管理机制
    学习进度第十二周
    十天冲刺10
    单词统计续
    十天冲刺9
    学习进度第十一周
    十天冲刺8
    十天冲刺7
  • 原文地址:https://www.cnblogs.com/alinh/p/9025858.html
Copyright © 2011-2022 走看看