实现字符串拼接(可变参数的)
因为工作需要,需要频繁用的字符串拼接,参数还不固定,所以写了下面的例子,算是给自己的记录
#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; }