CString、std::string格式化字符串
2019獨角獸企業(yè)重金招聘Python工程師標準>>>
=============================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();
?
?
附加說明
在C++有兩種 ??
? 一個是在<sstream>另一個是<strstream>它們實現(xiàn)的東西基本一樣, ? 區(qū)別是前者為前標準. ??
? 而前標準里ostringstream::str()是返回std::string的. ??
? ??
? <strstream>包含 ??
? ? class ? strstreambuf; ??
? ? class ? istrstream; ??
? ? class ? ostrstream; ??
? ? class ? strstream; ??
? 它們是基于char*編寫的 ??
? ??
? <sstream> ??
? class ? istringstream; ??
? class ? ostreamstream; ??
? class ? stringbuf; ??
? class ? stringstream; ??
? class ? ... ??
? .... ??
? 它們是基于std::string編寫的.?
轉(zhuǎn)載于:https://my.oschina.net/u/2314763/blog/419835
總結(jié)
以上是生活随笔為你收集整理的CString、std::string格式化字符串的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C#实现http断点下载
- 下一篇: (笔试题)将数组分成两组,使两组的和的差