C/C++使用fmt庫實現(xiàn)格式化字符串
實現(xiàn)的目的;提高 C/C++ 編譯速度,fmt 庫模板嵌套過多編譯速度非常慢,且編譯后程序體積也過大,函數(shù)步入的棧幀過多!
只支持格式;{}
不支持格式;{:02x}
class fmt
{
public:
template <typename S, typename ...T>
static std::string format(const S& fmt, T ... args) noexcept
{
std::string str;
if constexpr (std::is_same<S, std::string>::value)
{
str = fmt;
}
else if constexpr (std::is_same<S, std::string_view>::value)
{
str = std::string(fmt.data(), fmt.size());
}
else
{
str = fmt;
}
(..., format_string(str, args));
return str;
}
template <typename OutputIt, typename ...T>
static void format_to(OutputIt&& out, const std::string& fmt, T ... args)
{
std::string result = format(fmt, std::forward<T&&>(args)...);
for (char ch : result)
{
*out = ch;
}
}
private:
template <typename T>
static std::string to_string(const T& value) noexcept
{
if constexpr (std::is_same<T, bool>::value)
{
return value ? "true" : "false";
}
else if constexpr (std::is_pointer<T>::value)
{
using DECAY_T = typename std::decay<T>::type;
if constexpr (std::is_same<char*, DECAY_T>::value || std::is_same<const char*, DECAY_T>::value)
{
return value ? value : "";
}
else
{
if (value)
{
char buf[sizeof(value) << 2];
snprintf(buf, sizeof(buf), "%p", reinterpret_cast<const void*>(value));
return buf;
}
return "null";
}
}
else if constexpr (std::is_same<T, std::string>::value)
{
return value;
}
else if constexpr (std::is_same<T, std::string_view>::value)
{
return std::string(value.data(), value.size());
}
else
{
return std::to_string(value);
}
}
template <typename T>
static std::string to_string(const std::shared_ptr<T>& value) noexcept
{
return fmt::to_string(value.get());
}
template <typename T>
static void format_string(std::string& out, const T& value) noexcept
{
replace_string(out, "{}"sv, fmt::to_string(value));
}
public:
static bool replace_string(std::string& str, const std::string_view& old_string, const std::string_view& new_string) noexcept;
};
inline bool fmt::replace_string(std::string& str, const std::string_view& old_string, const std::string_view& new_string) noexcept
{
size_t pos = str.find(old_string);
if (pos == std::string::npos)
{
return false;
}
str.replace(pos, old_string.length(), new_string);
return true;
}方法補充
除了上文的方法,小編還為大家整理了其他C++格式化字符串的方法,希望對大家有所幫助
1.使用C++中的字符串流(stringstream)
例如:
#include <sstream> std::stringstream ss; int num = 42; const char* str = "hello"; ss << "Num: " << num << ", str: " << str; std::string result = ss.str();
上述代碼中,我們使用字符串流將數(shù)字和字符串插入到字符串中,得到了最終的格式化結果。
需要注意的是,當處理浮點數(shù)時,可能會出現(xiàn)精度誤差的問題,可以使用std::setprecision函數(shù)來指定小數(shù)點后的位數(shù)。
示例說明
下面是一個例子,展示了使用字符串流格式化浮點數(shù)的方法:
#include <sstream> #include <iomanip> std::stringstream ss; double num = 3.14159265358979323846; ss << "The value of pi is approximately " << std::setprecision(6) << std::fixed << num; std::string result = ss.str();
上述代碼中,我們使用字符串流將浮點數(shù)插入到字符串中,并設置小數(shù)點后6位的精度,輸出結果為:
The value of pi is approximately 3.141593.
2.使用format庫
format庫是C++11格式化字符串的一種解決方案,它的設計目標是提供簡單、便捷的格式化語法,并且不需要調用大量的函數(shù)來實現(xiàn)字符串的格式化。
format庫可以被看作是一個功能強大的printf的替代品。在使用上它更加靈活,更加安全,而且能夠提供更加友好的錯誤提示。
format庫的使用方式和Python的字符串格式化方式非常相似,適合需要頻繁使用字符串格式化的場景。
具體用法
#include <fmt/format.h>
#include <iostream>
int main(){
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");
return 0;
}到此這篇關于C/C++使用fmt庫實現(xiàn)格式化字符串的文章就介紹到這了,更多相關C++格式化字符串內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
C語言驅動開發(fā)之內核通過PEB獲取進程參數(shù)
PEB結構(Process Envirorment Block Structure)其中文名是進程環(huán)境塊信息。本文將通過PEB實現(xiàn)獲取進程參數(shù),感興趣的小伙伴可以了解一下2022-10-10
基于Qt播放器的實現(xiàn)詳解(支持Rgb,YUV格式)
這篇文章主要為大家詳細介紹了如何利用Qt實現(xiàn)簡易的播放器,可以支持支持Rgb,YUV格式。文中的示例代碼講解詳細,感興趣的小伙伴可以嘗試一下2022-12-12
C語言連續(xù)生成隨機數(shù)的實現(xiàn)方法
這篇文章主要介紹了C語言連續(xù)生成隨機數(shù)的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-01-01
C/C++?Qt?Tree與Tab組件實現(xiàn)分頁菜單功能
這篇文章主要介紹了C/C++?Qt?Tree與Tab組件實現(xiàn)分頁菜單功能,實現(xiàn)一個類似于樹形菜單欄的功能,當用戶點擊菜單欄中的選項時則會跳轉到不同的頁面上,本文簡單給大家分享實現(xiàn)代碼,感興趣的朋友跟隨小編一起看看吧2021-11-11

