string拼接不管是书写还是易读性都比较差, 现模仿QString arg()的写法,实现简单的拼接,直接上代码
#include "stdafx.h"
#include <string>
#include <stdarg.h>
#include <iostream>
#include <vector>
using namespace std;
class qtString
{
public:
//str 待格式化字符
qtString(std::string str) {
mpQtString = this;
mCount = 0;
mString = str;
}
//返回格式化后的字符
std::string getStdString()
{
return mString;
}
//参数
qtString arg(std::string param) {
replace(param);
mpQtString->mCount++;
return *mpQtString;
}
//替换字符 根据qt里面的用%0 %1 ....代表需要替换的参数
//必须是由%0起始 可自行完善此缺陷
void replace(std::string param)
{
auto key = "%" + to_string(mpQtString->mCount);
auto index = mString.find(key);
if (index < 0) return;
auto temp = mString.substr(0, index);
temp += param;
mpQtString->mString = mString.replace(0, index+key.length(), temp);
}
public:
qtString* mpQtString;
std::string mString;
//%x 计数器 arg 调用一次 mCount加1
int mCount;
};