String
1.0.0
一个简单的可自定义实现的字符串类实现,该类别模仿std :: String的功能,包括迭代器。
using size_type = size_t; // size_type for positions of char in cstring
using const_pointer = const_iterator;
using pointer = iterator;
using const_reverse_pointer = const_reverse_iterator;
using reverse_pointer = reverse_iterator;
String(); // default constructor
String(const String &); // copy constructor
String(const String& other, size_type pos, size_t len = npos); // substring constructor
String(String &&); // move constructor
String(const char *); // from c-string
String(const char* s, size_t n); // from buffer
String(size_t n, char c);// fill constructor
String(const const_iterator first, const const_iterator second); // range constructor
String & operator= (const String &); // copy assignment
String & operator = (String &&); // move assignment
~String();
将迭代器/const_iter返回到第一个字符
iterator begin();
const_iterator begin() const;
返回迭代器/const_iterator指向字符串的过去字符
iterator end();
const_iterator end() const;
返回指向字符串的第一个字符 /过去字符的const_iterator
const_iterator cbegin() const;
const_iterator cend() const;
返回reverse_iterator/const_reverse_iterator到最后一个字符
reverse_iterator rbegin();
const_reverse_iterator rbegin() const;
返回reverse_iterator/const_reverse_iterator指向字符串的过去末端字符
reverse_iterator rend();
const_reverse_iterator rend() const;
返回const_reverse_iterator指向字符串的最后一个字符 /反向过去字符
const_reverse_iterator crbegin() const;
const_reverse_iterator crend() const;
获取字符串的字符
const char & operator [] (size_type) const;
char & operator [] (size_type);
在字符串中获取字符
const char & at(size_type) const;
char & at(size_type);
访问第一个字符
const char & front() const;
char & front();
访问最后一个字符
const char & back() const;
char & back();
附加到字符串
String & operator += (const String &); // string (1)
String & operator += (const char *); // c - string (2)
String & operator += (char); // char (3)
附加到字符串
String& append(const String& str); // string (1)
String& append(const String& str, size_type subpos, size_t sublen = npos); // substring (2)
String& append(const char* s); // c - string (3)
String& append(const char* s, size_t n); // buffer(4)
String& append(size_type n, char c); // fill(5)
String& append(const const_iterator first, const const_iterator second); // range(6)
String & push_back(char);
插入字符串
String& insert(size_type pos, const String& other); // string(1)
String& insert(size_type pos, const String& other, size_type subpos, size_t sublen = npos); // substring(2)
String& insert(size_type pos, const char* other); // c - string(3)
String& insert(size_type pos, const char* s, size_t n); // buffer(4)
String& insert(size_type pos, size_t n, char c); // fill(5)
void insert(iterator p, size_t n, char c); // fill(6)
iterator insert(iterator p, char c); // single character(7)
void insert(iterator p, const const_iterator first, const const_iterator last); // range(8)
从字符串中删除字符
String& erase(size_type pos = 0, size_t len = npos); // sequence(1)
iterator erase(const_iterator p); // character(2)
iterator erase(const_iterator first, const_iterator last); // range(3)
更换字符串的部分
String& replace(size_type pos, size_t len, const String& other); // string(1)
String& replace(const_iterator i1, const_iterator i2, const String& other); // string(2)
String& replace(size_type pos, size_t len, const String& other, size_type subpos, size_t sublen = npos); // substring(3)
String& replace(size_type pos, size_t len, const char* s); // c - string(4)
String& replace(const_iterator i1, const_iterator i2, const char* other); // c - string(5)
String& replace(size_type pos, size_t len, const char* other, size_t n); // buffer(6)
String& replace(const_iterator i1, const_iterator i2, const char* other, size_t n); // buffer(7)
String& replace(size_type pos, size_t len, size_t n, char c); // fill(8)
String& replace(const_iterator i1, const_iterator i2, size_type n, char c); // fill(9)
String& replace(const_iterator i1, const_iterator i2, const_iterator first, const_iterator second); // range(10)
交换字符串值
void swap(String &);
删除最后一个字符
String & pop_back();
获取c字符串等效
const char * c_str() const;
从字符串复制字符序列
size_t copy(char* s, size_t len, size_type pos = 0) const;
在字符串中查找内容
size_type find(const String& other, size_type pos = 0) const; //string(1)
size_type find(const char* s, size_type pos = 0) const; // c - string(2)
size_type find(const char* s, size_type pos, size_type n) const; // buffer(3)
size_type find(char c, size_type pos = 0) const; // character(4)
在字符串中查找内容的最后出现
size_type rfind(const String& other, size_type pos = npos) const; // string(1)
size_type rfind(const char* s, size_type pos = npos) const; // c - string(2)
size_type rfind(const char* s, size_type pos, size_t n) const; // buffer(3)
size_type rfind(char c, size_type pos = npos) const; // character(4)
在字符串中找到字符
size_type find_first_of(const String& other, size_type pos = 0) const; // string(1)
size_type find_first_of(const char* other, size_type pos = 0) const; // c - string(2)
size_type find_first_of(const char* other, size_type pos, size_t n) const; // buffer(3)
size_type find_first_of(char c, size_type pos = 0) const; // character(4)
从末端找到字符串中的字符
size_type find_last_of(const String& other, size_type pos = String::npos) const; // string(1)
size_type find_last_of(const char* other, size_type pos = String::npos) const; // c - string(2)
size_type find_last_of(const char* other, size_type pos, size_t n) const; // buffer(3)
size_type find_last_of(char c, size_type pos = String::npos) const; // character(4)
在字符串中找到缺乏字符
size_type find_first_not_of(const String& other, size_type pos = 0) const; // string(1)
size_type find_first_not_of(const char* other, size_type pos = 0) const; // c - string(2)
size_type find_first_not_of(const char* other, size_type pos, size_t n) const; // buffer(3)
size_type find_first_not_of(char c, size_type pos = 0) const; // character(4)
从末尾找到字符串中的非匹配字符
size_type find_last_not_of(const String& other, size_type pos = String::npos) const; // string(1)
size_type find_last_not_of(const char* other, size_type pos = String::npos) const; // c - string(2)
size_type find_last_not_of(const char* other, size_type pos, size_t n) const; // buffer(3)
size_type find_last_not_of(char c, size_type pos = String::npos) const; // character(4)
生成子字符串
String substr(size_type pos = 0, size_t len = npos) const;
返回字符串长度
size_t length() const;
size_t size() const;
返回字符串的最大尺寸
size_t max_size() const;
调整字符串
void resize(size_t n);
void resize(size_type n, char c);
分配存储的返回大小
size_t capacity() const;
要求更改容量
void reserve(size_t n = 0);
清除字符串
void clear();
返回true是字符串是空的
bool empty() const;
收缩适合
void shrink_to_fit();
static const size_t npos = -1; // Maximum value for size_t
比较
// Size is compared and if equal then comapred lexicographically
friend bool operator == (const String &, const String &);
friend bool operator != (const String &, const String &);
friend bool operator < (const String &, const String &);
friend bool operator > (const String &, const String &);
friend bool operator <= (const String &, const String &);
friend bool operator >= (const String &, const String &);
交换两个字符串的值
void swap(String& x, String& y);
返回串联字符串
String operator+ (const String& lhs, const String& rhs);
String operator+ (const String& lhs, const char* rhs);
String operator+ (const char* lhs, const String& rhs);
String operator+ (const String& lhs, char rhs);
String operator+ (char lhs, const String& rhs);
将字符串插入流中
std::ostream& operator<< (std::ostream& os, const _JD String& str);
从流中提取字符串
std::istream& operator>> (std::istream& is, _JD String& str);
将线从流到字符串中获取
std::istream& getline(std::istream& is, _JD String& str, char delim);
std::istream& getline(std::istream& is, _JD String& str);