C++ 模板写的短小字符串类,用于替换字符数组和std::string

时间:2023-03-31 17:48:05
【文件属性】:

文件名称:C++ 模板写的短小字符串类,用于替换字符数组和std::string

文件大小:9KB

文件格式:HPP

更新时间:2023-03-31 17:48:05

C++ 模板 短小字符串类 替换字符数组 std::string

//短小字符串类 template struct TinyString { TinyString(); template TinyString(const TinyString& str); TinyString(size_t n, char c); TinyString(const char* s, size_t n=0); TinyString(const std::string& str); template TinyString& operator = (const TinyString& str); TinyString& operator = (const std::string& str); TinyString& operator = (const char* s); TinyString& operator = (char ch); template TinyString& operator += (const TinyString& other); bool empty() const { return len == 0; } int size() const { return len; } int obj_size() const { return sizeof(*this); } const char* GetData()const { return buffer; } const char* c_str()const { return buffer; } bool operator == (const TinyString& other) const; bool operator < (const TinyString& other) const; operator std::string() const; //转为字符串 std::string ToString()const; //转为字符串 template friend std::ostream & operator<< (std::ostream& os, const TinyString& str); template friend bool operator == (const TinyString& s1, const TinyString& s2); //...... uint8_t len; char buffer[N-1]; }; typedef TinyString<8> TinyStr8; typedef TinyString<16> TinyStr16; typedef TinyString<32> TinyStr32; typedef TinyString<1024> TinyStr1K; typedef TinyString<4096> TinyStr4K;


网友评论