C++ 如何做到字符串插值?

C++有类似于Swift的字符串插值的方式吗?

println("The current value of friendlyWelcome is $(friendlyWelcome)");

要编译时就处理。
预处理成类似于以下的代码:

cout<<"The current value of friendlyWelcome is "<< friendlyWelcome << ""<< endl;

据我所知,没有这方式

snprintf()等格式化字符串不知道是否满足你的要求


  char str[10]={0,};
     snprintf(str, sizeof(str), "0123456789012345678");

可以使用宏定义来试试,例如:

 #define ERRORMSG(cond,printf_exp)     \
   ((cond)?(NKDbgPrintfW(L"ERROR: %s line %d: ",__WFILE__,__LINE__), NKDbgPrintfW printf_exp),1:0)

没有你说的那种方法,可以试试如下程序,希望有帮助!

#include "StdAfx.h"
#include
#include
using namespace std;

int main(void)
{
string str,str1,str2;
int index;

//判断截取的子串是否由blanks组成
str="   cjc is a master.";
str1="cjc is a master.";
index=str.find("cjc");
str.assign(str.c_str(),index);

if(str.find_first_not_of(" ")==-1)
{
    cout<<"The string is constructed of blanks..."<<endl; }="" else="" {="" cout<<str<<endl;="" 在字符串中插入子串="" index="str1.find("master");" str2="str1.substr(index);" str1.assign(str1.c_str(),index);="" str="str1+"good" "+str2;="" system("pause");="" return="" 0;="" }<="" pre=""><img src="http://www.2cto.com/uploadfile/Collfiles/20140507/20140507091544383.jpg" alt="" style="width: 341px; height: 90px;"><br>                        </endl;></iostream></string>

没有,但可以动态查找字符串,切割,合并

用 printf不行么?
printf("The current value of friendlyWelcome is %s", friendlyWelcome)