網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
我這個(gè)人總是喜歡在寫(xiě)代碼時(shí)追求極致,比如總是糾結(jié)于變量的命名,內(nèi)存的消耗,執(zhí)行的效率,接口的便捷性,代碼的可擴(kuò)展性。。。但很多時(shí)候需要在他們之間做取舍,這就導(dǎo)致我在編碼時(shí)經(jīng)常陷入僵局,唉。。。真是程序員的可悲,為此幾年前我還專門(mén)將自己的CSDN簽名改成了現(xiàn)在這樣。
今天我又帶來(lái)一個(gè)函數(shù),相比網(wǎng)上其他版本效率更高(不存在額外拷貝問(wèn)題),使用更便捷(無(wú)需預(yù)先分配緩存)。
起初我設(shè)計(jì)的函數(shù)如下:相比網(wǎng)上其他的Format,特點(diǎn)是降低了內(nèi)存消耗,也提升了使用的便捷性,但帶來(lái)了執(zhí)行效率的下降,而更嚴(yán)重的是存在多線程隱患,不推薦使用。
const std::string& StrUtil::Format(const char* pszFmt, ...) { va_list body; va_start(body, pszFmt); int nChars = _vscprintf(pszFmt, body); std::mutex mtx; mtx.lock(); static std::string str; // 非線程安全,因此下面使用互斥鎖 str.resize(nChars + 1); vsprintf((char*)str.c_str(), pszFmt, body); mtx.unlock(); va_end(body); return str; // 非線程安全 }
然后,我又設(shè)計(jì)出了第二個(gè)Format方案。上個(gè)方案之所以在函數(shù)內(nèi)部使用了static變量,是為了解決函數(shù)返回后變量“str”銷毀的問(wèn)題,這也是能讓一個(gè)Format好用的關(guān)鍵問(wèn)題所在——“如何能在函數(shù)返回后,構(gòu)建好的字符串仍然能夠在內(nèi)存短暫駐留”,如下(利用臨時(shí)對(duì)象特性保證內(nèi)存短暫駐留)
/************************************************************************* ** Desc : 好用的格式化字符串“函數(shù)”,使用方法: ** printf(StrUtil::Format("%,%s", "hello", "world").c_str()); ** Param : [in] pszFmt ** : [in] ... ** Return : std::string ** Author : xktesla *************************************************************************/ class StrUtil { public: struct Format : std::string { public: Format(const char* pszFmt, ...) { va_list body; va_start(body, pszFmt); int nChars = _vscprintf(pszFmt, body); this->resize(nChars + 1); vsprintf((char*)this->c_str(), pszFmt, body); va_end(body); } private: Format() = delete; Format(const Format&) = delete; Format& operator=(const Format&) = delete; }; };
原文鏈接:https://blog.csdn.net/xk641018299/article/details/122153434
相關(guān)推薦
- 2022-06-12 Python中常用的內(nèi)置函數(shù)_python
- 2022-09-17 使用cache加快編譯速度的命令詳解_相關(guān)技巧
- 2022-07-27 Python?迭代器Iterator詳情_(kāi)python
- 2022-06-21 Android實(shí)現(xiàn)登陸界面的記住密碼功能_Android
- 2022-06-01 Snort中pcre和正則表達(dá)式的使用詳解_正則表達(dá)式
- 2022-10-03 Pandas數(shù)據(jù)集的分塊讀取的實(shí)現(xiàn)_python
- 2022-05-17 基于Python編寫(xiě)簡(jiǎn)易文字語(yǔ)音轉(zhuǎn)換器_python
- 2022-04-15 利用Python實(shí)現(xiàn)數(shù)值積分的方法_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支