
实现的目的提高 C/C 编译速度fmt 库模板嵌套过多编译速度非常慢且编译后程序体积也过大函数步入的栈帧过多只支持格式{}不支持格式{:02x}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102classfmt{public:templatetypenameS,typename...Tstaticstd::string format(constS fmt, T ... args) noexcept{std::string str;ifconstexpr (std::is_sameS, std::string::value){str fmt;}elseifconstexpr (std::is_sameS, std::string_view::value){str std::string(fmt.data(), fmt.size());}else{str fmt;}(..., format_string(str, args));returnstr;}templatetypenameOutputIt,typename...Tstaticvoidformat_to(OutputIt out,conststd::string fmt, T ... args){std::string result format(fmt, std::forwardT(args)...);for(charch : result){*out ch;}}private:templatetypenameTstaticstd::string to_string(constT value) noexcept{ifconstexpr (std::is_sameT,bool::value){returnvalue ?true:false;}elseifconstexpr (std::is_pointerT::value){usingDECAY_T typenamestd::decayT::type;ifconstexpr (std::is_samechar*, DECAY_T::value || std::is_sameconstchar*, DECAY_T::value){returnvalue ? value :;}else{if(value){charbuf[sizeof(value) 2];snprintf(buf,sizeof(buf),%p,reinterpret_castconstvoid*(value));returnbuf;}returnnull;}}elseifconstexpr (std::is_sameT, std::string::value){returnvalue;}elseifconstexpr (std::is_sameT, std::string_view::value){returnstd::string(value.data(), value.size());}else{returnstd::to_string(value);}}templatetypenameTstaticstd::string to_string(conststd::shared_ptrT value) noexcept{returnfmt::to_string(value.get());}templatetypenameTstaticvoidformat_string(std::string out,constT value) noexcept{replace_string(out,{}sv, fmt::to_string(value));}public:staticboolreplace_string(std::string str,conststd::string_view old_string,conststd::string_view new_string) noexcept;};inlineboolfmt::replace_string(std::string str,conststd::string_view old_string,conststd::string_view new_string) noexcept{size_tpos str.find(old_string);if(pos std::string::npos){returnfalse;}str.replace(pos, old_string.length(), new_string);returntrue;}方法补充除了上文的方法小编还为大家整理了其他C格式化字符串的方法希望对大家有所帮助1.使用C中的字符串流stringstream例如123456#include sstreamstd::stringstream ss;intnum 42;constchar* str hello;ss Num: num , str: str;std::string result ss.str();上述代码中我们使用字符串流将数字和字符串插入到字符串中得到了最终的格式化结果。需要注意的是当处理浮点数时可能会出现精度误差的问题可以使用std::setprecision函数来指定小数点后的位数。示例说明下面是一个例子展示了使用字符串流格式化浮点数的方法123456#include sstream#include iomanipstd::stringstream ss;doublenum 3.14159265358979323846;ss The value of pi is approximately std::setprecision(6) std::fixed num;std::string result ss.str();上述代码中我们使用字符串流将浮点数插入到字符串中并设置小数点后6位的精度输出结果为The value of pi is approximately 3.141593.2.使用format库format库是C11格式化字符串的一种解决方案它的设计目标是提供简单、便捷的格式化语法并且不需要调用大量的函数来实现字符串的格式化。format库可以被看作是一个功能强大的printf的替代品。在使用上它更加灵活更加安全而且能够提供更加友好的错误提示。format库的使用方式和Python的字符串格式化方式非常相似适合需要频繁使用字符串格式化的场景。具体用法1234567891011#include fmt/format.h#include iostreamintmain(){std::cout fmt::format({} {} {}!\n,Hello,World, 123);std::cout fmt::format({2} {1} {0}!\n,Hello,World, 123);std::cout fmt::format({0:} {0:-} {0:#}\n, 123);std::cout fmt::format({0:.3f} {0:7.3f}\n, 3.1415926535);std::cout fmt::format({{ }}\n);return0;}到此这篇关于C/C使用fmt库实现格式化字符串的文章就介绍到这了