使用std:string时怎么实现类似MFC中CString.Format的功能
展开全部
=============================CString==================================
当有多个字串时,比如
int n1 = 5;
int n2 = 10;
char sz1[] = "abcdefg";
char sz2[] = "hijklmn";
用std中的string如何写出最简单的代码得到MFC中CString如下效果:
CString s;
s.Format(" result: %d + %d = %d/n sz1: %s/n sz2: %s/n", n1, n2, n1+n2, sz1, sz2 );
===========================std::string==================================
int n1 = 5;
int n2 = 10;
char sz1[] = "abcdefg";
char sz2[] = "hijklmn";
std::ostringstream ostr; // include <sstream>
ostr << "result:" << n1 << "+" << n2 << "=" << n1+n2
<< "/nsz1:" << sz1
<< "/nsz2:" << sz2;
//std::cout << ostr.str().c_str() << std::endl;
std::string s = ostr.str();
当有多个字串时,比如
int n1 = 5;
int n2 = 10;
char sz1[] = "abcdefg";
char sz2[] = "hijklmn";
用std中的string如何写出最简单的代码得到MFC中CString如下效果:
CString s;
s.Format(" result: %d + %d = %d/n sz1: %s/n sz2: %s/n", n1, n2, n1+n2, sz1, sz2 );
===========================std::string==================================
int n1 = 5;
int n2 = 10;
char sz1[] = "abcdefg";
char sz2[] = "hijklmn";
std::ostringstream ostr; // include <sstream>
ostr << "result:" << n1 << "+" << n2 << "=" << n1+n2
<< "/nsz1:" << sz1
<< "/nsz2:" << sz2;
//std::cout << ostr.str().c_str() << std::endl;
std::string s = ostr.str();
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询